0

I have a XML Response as below where it contains CDATA as wells as xml element with same name

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetISDResponse xmlns="http://www.webserviceX.NET">
         <GetISDResult><![CDATA[<NewDataSet>
  <Table>
    <code>355</code>
    <name>Albania</name>
  </Table>
  <Table>
    <code>355</code>
    <name>Albania</name>
  </Table>
</NewDataSet>]]></GetISDResult>
      </GetISDResponse>
   </soap:Body>
</soap:Envelope>

How can i read the value of code and name xml element in soapui Groovy. I have tried all type of suggestions from various blogs but didnt worked out

Also I am using SOAPUI 5.3.0 freeware and not SOAPUI PRO.

Rao
  • 20,781
  • 11
  • 57
  • 77
Divakar Ragupathy
  • 191
  • 1
  • 6
  • 20

2 Answers2

1

The XML parser will deliver the content of the CDATA section as a simple text node. You need to extract the string value of the CDATA section and parse it again.

I don't know why people put XML inside CDATA sections like this - it's a perverse thing to do, and if you have any influence over the people who designed the XML, get them to change their ways. Meanwhile, you have to parse the content twice.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

It is required to extract cdata part and then parse it fetch the required data.

You can use below Script Assertion

def response = context.response

assert response, 'response is empty or null'

//Closure to parse and extract the data
def getData = { data, element ->
  def pXml = new XmlSlurper().parseText(data)
  def codes = pXml.'**'.findAll { it.name() == element}  
}

//Get the cdata part
def cdata = getData(response, 'GetISDResult')[0] as String
log.info cdata

//Get the table data as map
def tableMap = getData(cdata, 'Table').inject([:]){m, item -> m[item.code.text()] = item.name.text();m}
log.info tableMap

tableMap.each { log.info "code : ${it.key} and name : ${it.value}" }

You can quickly try online Demo

Rao
  • 20,781
  • 11
  • 57
  • 77