Extract node from xml

I do not understand why this code cannot get a specific node from an xml string. The code below will cause this error: groovy.util.slurpersupport.NodeChildren.attributes () is applicable for argument types :() values: []

Thanks for any explanation how to solve this!

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" && it.@name=="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)
    }          
}     
+4
source share
1 answer

Because inputBindings- this is a set of nodes - there may be several inputBindings. The corrected code is below:

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" && it.@name=="MergeLogData"}.each{activity->
   traverse(activity.inputBindings[0]) 
}

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)
    }          
} 
+4
source

Source: https://habr.com/ru/post/1569108/


All Articles