I have a very simple ( I thought ) xml file like this...
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Things>
<thing indexNum='1'>
<a>123</a>
<b>456</b>
<c>789</c>
</thing>
<thing indexNum='2'>
<a>123</a>
<b>456</b>
<c>789</c>
</thing>
</Things>
The issue I'm facing is that I cannot simply get at each node separately with this code... it is printing ALL of the things, and what I'm really attempting to do is to collect each node into a map, then interrogate/transform some key/value pairs in the map and replace them (way down the road, I know..)
Here's my horrendous code... any chance someone can set me in the right direction?
def counter = 0
Things.thing.each { tag ->
counter++
println "\n-------------------------------- $counter ------------------------------------"
Things.thing.children().each { tags ->
println "$counter${tags.name()}: $tags"
return counter
}
println "\n$counter things processed...\n"
}
Would it be easier to manipulate this inside of a map? (I generated this xml with a map in the first place, thinking that there would be some easy methods to work with the XML... I'm starting to wonder after goofing around for days and getting basically nowhere)
Thanks and Regards
The reason you keep getting the inner nodes is because you incorrectly iterate over the outer list twice. The inner loop should iterate only over tag
:
doc = new XmlSlurper().parse("things.xml")
doc.thing.each { thing ->
println "thing index: ${thing.@indexNum}"
thing.children().each { tag ->
println " ${tag.name()}: ${tag.text()}"
}
}
Output:
thing index: 1
a: 123
b: 456
c: 789
thing index: 2
a: 123
b: 456
c: 789
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With