Ask a Question

Extracting data from CData in SOAP Response

Sphujidhwaj
Contributor

Extracting data from CData in SOAP Response

I have a Soap Request XML which takes JsonString as input and in the output I get the response XML in the below format.

 

<ReportResponse xmlns="http://tempuri.org/">
         <ReportResult><![CDATA[{
  "Report": [
    {
      "Name": "ABC",
      "Number": "123",
      "Type": "XYZ",
      "Desc": "EFGH",
      "Group Name": "Name123",
      "Group Number": "123",
      "End Date": "12/30/2014",
      "Due Date": "04/15/2015",
      "Completion Date": null,
      "Status": "Status1",
      "Received Date": "",
      "Delivery Date": "",
      "Location": "",
      "Role": "",
     },
	 {
      "Name": "EFG",
      "Number": "123",
      "Type": "XYZ",
      "Desc": "EFGH",
      "Group Name": "Name123",
      "Group Number": "123",
      "End Date": "12/30/2014",
      "Due Date": "04/15/2015",
      "Completion Date": null,
      "Status": "Status1",
      "Received Date": "",
      "Delivery Date": "",
      "Location": "",
      "Role": "Manager",
     },

I want to extract the names which are more than 500. What is the way to extract names?  

"Name": "ABC",

Please help me.

23 REPLIES 23
rupert_anderson
Valued Contributor

Hi,

 

Where you say names that are more than 500, what exactly do you mean? And how do you want the output of the extraction to be?

 

For eaxample, if you mean extract JSON documents (maybe log or store in a variable) which have a 'number' property >500 then that should be fairly OK to do in a Groovy script.

 

Let me know,

 

Rup

Author of SoapUI Cookbook

Hi Rup,

 

When I say names more than 500 it means the count of names,I have more than 500 blocks or chunks of names to be extracted.

Still I have not got there yet I am first trying to extract the data from the CData 

But encountering the following message.

 

groovy.lang.MissingPropertyException: No Such Property: messageExchange for class: Script8 error at line 3
import com.eviware.soapui.support.XmlHolder

respXmlHolder = new xmlHolder(messageExchange.getResponseContentAsXml())
respXmlHolder.declareNamespace("ns1","http://tempuri.org/")
CDATAXml = respXmlHolder.getNodeValue("//ns1:Result")
log.info(CDATAXml)

Please let me know where I am going wrong. Are there any links for the same where I can get some assistance in debugging such errors. 

 

Thanks,

SD

 

 

first of all it should be 'XmlHolder', and is case sensitive.


Regards,
Rao.

Hi SD,

 

Ok, so basically if the count of those Report JSON documents goes above 500, then we capture 501+ etc.

 

OK, maybe we should work on it a step at a time.

 

1) First we can try to extract the report JSON documents from the response CDATA.

2) Then, we can use JSON Slurper to parse the JSON data into report objects.

3) Then we can try to extract just the report objects where the count is >500

 

In terms of the error you're getting, at first glance it looks like you're not running the script from a Script Assertion? The messsageExchange variable is only available to script run there:

 

Screen Shot 2016-03-16 at 10.25.31.png

 

 

Author of SoapUI Cookbook

Thats a nice JSON Slurper script example that Rao has provided, that should cover step 2 and part of step 3 🙂

Author of SoapUI Cookbook

Yes it is in Caps as shown below

 

respXmlHolder = new XmlHolder(messageExchange.getResponseContentAsXml())

Try this: 

groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
holder = groovyUtils.getXmlHolder("RequestStepName#ResponseAsXml"); //Change the RequestStepName 

Hi,

 

 

CDATAXml = respXmlHolder.getNodeValue("//ns1:Result")
log.info(CDATAXml)
def data = new XmlSlurper().parseText(CDATAXml)
def list = new groovy.json.JsonSlurper().parseText( data.toString())
log.info list.size()

When I try to run the above script I get an error "Content is not allowed in prolog"

 

The CDATAXml is very huge (I copied the content in text file it comes around 12000 lines)

 

Is the error due to huge data? 

Please let me know if I am going wrong anywhere.

 

Thanks,

SD

 

 

cancel
Showing results for 
Search instead for 
Did you mean: