0
votes

"I have a code that is working as expected but now I have to find the element in different format. Example is below

<car-load>
 <car-model model="i10">
    <model-year>
        <year.make>
            <name>corolla</name>
        </year.make>
    </model-year>
 </car-model>
</car-load>

I have to find the value of "corolla" from this XML. Please reply.

1
Sounds homeworky. And who the heck put's periods in XML tag names!? - billjamesdev

1 Answers

2
votes

You can run this in the Groovy console

def text = '''
<car-load>
 <car-model model="i10">
    <model-year>
        <year.make>
            <name>corolla</name>
        </year.make>
    </model-year>
 </car-model>
</car-load>'''


def records = new XmlSlurper().parseText(text)

// a quick and dirty solution
assert 'corolla' == records.toString()

// a more verbose, but more robust solution that specifies the complete path 
// to the node of interest
assert 'corolla' == records.'car-model'.'model-year'.'year.make'.name.text()