cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to remove CDATA from response not working

SOLVED
axasmgaxa
Occasional Contributor

Trying to remove CDATA from response not working

Hi All,

 

I am hoping someone can help me here! 

 

I have have a REST request that is returning a response, which seems to be in JSON format. I don't know much about JSON at all.

 

I can see the XML response also, which contains the "<![CDATA[" tag. 
I want to remove the CDATA tag, so that I can use the "Outline" tab to extract necessary data from the response.

I am using an event handler to replace all; in order to remove the tag but it isn't removing it.

The strange part of this is, when I try and remove any other text, that text is removed, which proves the event handler is working.

It just won't remove the CDATA tag.

Any help would be greatly appreciated!

 

I will attach a snip of the XML tab, and also the code I have in the event handler is below.

The event handler is "RequestFilter.afterRequest"

 

def content = context.httpResponse.responseContent

content = content.replaceAll( "<!\\[CDATA\\[", "" )
content = content.replaceAll( "]]>", "" )

log.info( content )

context.httpResponse.responseContent = content

 

Thanks in advance.

Steven.

1 ACCEPTED SOLUTION

Accepted Solutions
axasmgaxa
Occasional Contributor

Re: Trying to remove CDATA from response not working

Oh yes sure!

So the response was returned in JSON; which contained 3 nodes.

The 2nd node contained the XML I needed to extract my necessary data; called "suite_id"

 

I set up a property called "XMLData"; and a property transfer. 

I used the Property Transfer "wizard" to select the 2nd JSON node, via the XPATH path language.

I transferred this to the XMLData Property; so this now contains the XML response.

 

From here I set up a new property, "SuiteID", and another property transfer.

Using the new property transfer, I was able to extract the SuiteID data from the XMLData property. Again using the XPath language in the property transfer wizard.

I transferred the data to my "SuiteID" property. 

View solution in original post

9 REPLIES 9
nmrao
Community Hero

Re: Trying to remove CDATA from response not working

It is not appropriate to just replace CDATA sting literals always.

You may see how to deal with it in the below example. Please check if that helps!
https://github.com/nmrao/soapUIGroovyScripts/blob/804652073a8e7c1259bc567c7bf3d2a71e2f4254/groovy/xm...


Regards,
Rao.
nmrao
Community Hero

Re: Trying to remove CDATA from response not working

You may also see other examples dealing with cdata
https://github.com/nmrao/soapUIGroovyScripts/search?q=cdata


Regards,
Rao.
axasmgaxa
Occasional Contributor

Re: Trying to remove CDATA from response not working

Thanks for the reply @nmrao , when you say it is not appropriate; is that because it won't work?


I hope to remove the CDATA tags so that the XML will format properly; can be viewed in the "Outline" tab, and I can manipulate the formatted XML to extract data as needed, as the software was designed to do.

 

If that is not possible I can look at other solutions, but for now, that's what I am aiming to do.

nmrao
Community Hero

Re: Trying to remove CDATA from response not working

As stated earlier, it may not always work.
Inside CDATA
JSON can include xml
XML can include Json / xml

In order to be able to parse and read data, you can use the link given in the previous replies.

You can find more information on CDATA
https://stackoverflow.com/questions/2784183/what-does-cdata-in-xml-mean


Regards,
Rao.
axasmgaxa
Occasional Contributor

Re: Trying to remove CDATA from response not working

Hi @nmrao 

 

thanks again for taking the time to respond.

I moved to another solution for my problem using the property transfer steps to extract the data I need from the response XML. 

Seems to work just fine!

 

Kind Regards,

Steven.

nmrao
Community Hero

Re: Trying to remove CDATA from response not working

Good to hear.


Regards,
Rao.
nmrao
Community Hero

Re: Trying to remove CDATA from response not working

It would be great to share the approach or solution.


Regards,
Rao.
axasmgaxa
Occasional Contributor

Re: Trying to remove CDATA from response not working

Oh yes sure!

So the response was returned in JSON; which contained 3 nodes.

The 2nd node contained the XML I needed to extract my necessary data; called "suite_id"

 

I set up a property called "XMLData"; and a property transfer. 

I used the Property Transfer "wizard" to select the 2nd JSON node, via the XPATH path language.

I transferred this to the XMLData Property; so this now contains the XML response.

 

From here I set up a new property, "SuiteID", and another property transfer.

Using the new property transfer, I was able to extract the SuiteID data from the XMLData property. Again using the XPath language in the property transfer wizard.

I transferred the data to my "SuiteID" property. 

View solution in original post

nmrao
Community Hero

Re: Trying to remove CDATA from response not working

Thanks for sharing the solution.
But one thing is not clear that how the data inside CDATA is extracted which was original issue.

Anyway, I marked your post as solution as per your replies.


Regards,
Rao.
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors