I have the following xml:
<list>
<cars>
<model>2012</model>
<make>GM</make>
</cars>
</list>
I want to print these values as path:value as shown below.
list/cars/model : 2012
list/cars/make : GM
How can I achieve this? I tried the name()
method but it only prints the name of child item. I want to print the whole path till the element.
I can only use xmlSlurper parser to do this.
Thanks.
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 forlocalText()
. IflocalText()
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 simplejoin()
would give the required format.Above has been tested successfully in Groovy 2.4.5. If
localText()
is not available inNodeChild
then a version of Groovy older than 2.3.0 would be reason because that method has been added since 2.3.0