Syed
Syed

Reputation: 2607

Not able to fetch CDATA values of xml in groovy

I'm having a xml tag, and I want to get the internal values of CDATA.. I'm not able to get the values. Below in the xml and code

def response = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
  <getResponse xmlns="http://tempuri.org/">
     <getResult><![CDATA[<IDV><amount_min>347974</amount_min></IDV>]]></getResult>
  </getResponse>
   </soap:Body>
</soap:Envelope>'''

My code is

new XmlSlurper().parseText(response)?.Body?.getResponse?.getResult?.amount_min?.text();

Anything wrong in code?

Upvotes: 3

Views: 1337

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You need to parse the CDATA bit again. As it's in a CDATA tag, it's not parsed as XML, it's treated as a String by the original parse:

def response = '''<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
  <getResponse xmlns="http://tempuri.org/">
     <getResult><![CDATA[<IDV><amount_min>347974</amount_min></IDV>]]></getResult>
  </getResponse>
   </soap:Body>
</soap:Envelope>'''

def cdata = new XmlSlurper().parseText(response).Body.getResponse.getResult.text()
new XmlSlurper().parseText(cdata).amount_min

Upvotes: 2

Related Questions