def xml = '''
<list>
<cars>
<model>2012</model>
<make>GM</make>
<color>Gold</color>
</cars>
</list>
'''
def item = new XmlSlurper().parseText(xml)
item.'**'.inject([]) { acc, val ->
def localText = val.localText()
acc << val.name()
if( localText ) {
println "${acc.join('/')} : ${localText.join(',')}"
acc = acc.dropRight(1) // or acc = acc[0..-2]
}
acc
}
This would print as required. Above is using a depthFirst()
search on the tree and using inject (just not to mutate any other list) and looks for localText()
. If localText()
is encountered indicating the value of the leaf node, then print the path and the value. The path has been accumulated in a list which was used in inject. A simple join()
would give the required format.
Above has been tested successfully in Groovy 2.4.5. If localText()
is not available in NodeChild
then a version of Groovy older than 2.3.0 would be reason because that method has been added since 2.3.0