1

I'm Facing an issue with iteration of below xml because it neglects some part. Can anyone guide me through this

<?xml version="1.0" encoding="UTF-8"?>
<fruits type="array">
    <fruit>
        <name>Apple</name>
        <alias>App</alias>
        <colors>
            <color>Red</color>
            <color>Green</color>
        </colors>
        <available-in type="array">
            <country>
                <name>Ind</id>
            </country>
            <country>
                <name>Ind</id>
            </country>
        </available-in>
    </fruit>
</fruits>

please find the Groovy code I used below

def map = new XmlSlurper().parseText(response).children().collectEntries{n->[(n.name()):{->n.children()?.collectEntries(owner)?:n.text()}()]};
cfrick
  • 35,203
  • 6
  • 56
  • 68

1 Answers1

1

You can use groovy's GPath capability called depthFirst() to iterate over all the children like so:

def map = new XmlSlurper().parseText(response).depthFirst().collectEntries{[(it.name()): it.text()]}
Oren Yosifon
  • 897
  • 8
  • 22