0

"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 Answers1

2

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()
Dónal
  • 185,044
  • 174
  • 569
  • 824