4
我不明白爲什麼這段代碼無法從xml字符串中獲取某個節點。 下面的代碼會拋出這個錯誤:groovy.util.slurpersupport.NodeChildren.attributes()是適用於參數類型:()值:[]從xml中獲取節點
感謝任何解釋如何解決這個!
def xml ='''<ProcessDefinition>
<activity name="MergeLogData">
<inputBindings>
<SubmitLogMsgRequest>
<MsgLevel>
<value-of select="$Start/SubmitLogMsgRequest/MsgLevel"/>
</MsgLevel>
<for-each select="SubmitLogMsgRequest/LogMsg">
<LogMsg>
<for-each select="ErrorReport">
<ErrorReport>
<MsgCode>
<value-of select="MsgCode"/>
</MsgCode>
</ErrorReport>
</for-each>
</LogMsg>
</for-each>
</SubmitLogMsgRequest>
</inputBindings>
</activity>
</ProcessDefinition>'''
groovy.util.slurpersupport.GPathResult Process = new XmlSlurper().parseText(xml)
Process.depthFirst().grep { it.name()=="activity" && [email protected]=="MergeLogData"}.each{activity->
traverse(activity.inputBindings) // why is it not possible to point to the sub node "inputBindings" here ??
//traverse(activity) this would work, but its pointing to the wrong node, I want it to be "inputBindings"
}
public void traverse(node) {
StringBuffer ret = new StringBuffer()
node.attributes().each(){attribute ->
println "ATTRIBUTE VALUE: $attribute.value"
}
node.children().each {child->
if(child.name().length()>0)
println "NODE name: ${child.name()}"
traverse(child)
}
}