The problem is that this piece of code:
nodes.children().collectEntries {
[it.name(), it.childNodes() ? convertToMap(it) : it.text() ]
}
overrides the value in the resulting map. I didn't manage to find an elegant solution to it without doing some ugly hacks. But here is my solution:
final xml = """
<body>
<head>test</head>
<test>
<child>Child</child>
</test>
<tail>
<name>1</name>
<name>2</name>
<name>3</name>
<name>4</name>
<name>5</name>
</tail>
</body>
"""
def slurper = new XmlSlurper().parseText(xml)
println convertToMap(slurper)
def convertToMap(nodes) {
final list = []
final children = nodes.children().iterator()
while (children.hasNext()) {
final child = children.next()
list << [(child.name()): child.childNodes() ? convertToMap(child) : child.text()]
}
final keys = list.collect { it.keySet()[0].toString() }
if (keys.size() == keys.unique().size()) {
list.collectEntries { [(it.keySet()[0]): it[it.keySet()[0]]] }
} else {
list
}
}
What I'm doing here is that I first collect all the children as list of map entries, so it looks like [[key1:value1], [key2:value2]]
. Then I loop over this intermediate structure and gather the results.
I hope it helps to move forward. Maybe later someone will come to you with a better solution, because as I said, at the moment I haven't found any elegant way to solve it.