praveen2609
praveen2609

Reputation: 233

parse xml string response in grails

response xml text is :

"<?xml version="1.0" encoding="UTF-8"?><MMP><MERCHANT><RESPONSE><url>http://XXXXXXx/asdasd/asdsd/as</url><param name="ttype">QEQW</param><param name="tempTxnId">155411</param><param name="token">v22mM1NmwpHCKLp%2FZJC%2B7PUKASXr2aS01JPi2ZXDqT0%3D</param><param name="txnStage">1</param></RESPONSE></MERCHANT></MMP>"

I want to parse this xml text in grails/groovy. How do I iterate through all the elements to fetch the data of a particular element?

Upvotes: 0

Views: 3344

Answers (1)

aiolos
aiolos

Reputation: 4697

You can use the XMLSlurper to read the xml.

Here is some example code for your posted xml:

def response = new XmlSlurper().parseText(xml)

// accessing a specific element
println response.MERCHANT.RESPONSE.url
// iterating through a collection
response.MERCHANT.RESPONSE.param.each{println it}
// searching for specific elements
println response.MERCHANT.RESPONSE.param.find{it.@name == 'ttype'}

Upvotes: 3

Related Questions