Lauren
Lauren

Reputation: 13

Replace <![CDATA[ and ]]> with "" in SOAP response using Groovy

I have researched this question however I can't find the solution to when the entire Soap XML response is completely wrapped in <![CDATA[ and ]]>.

I keep running into the error message "Unexpected element: CDATA" when I'm trying to do a simple replace <![CDATA[ and ]]> with "" and I understand why but I am struggling with the code to get around it.

Here is what I have so far:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Request 1#Response" )

if( holder == null )
   {return} 
   Else
   {
    def responseContent = holder.response.responseContent
    responseContent = responseContent.replaceAll( "<!\\[CDATA\\[", "" )
    responseContent = responseContent.replaceAll( "]]>", "" )
    log.info( responseContent )
   }

I am using SoapUI and have visited many websites to try to figure it out, here some (A lot of them left me unclear and/or aren't for xml):

https://community.smartbear.com/t5/SoapUI-Pro/Resolved-Get-rid-of-unwanted-CDATA-in-request/td-p/34964

How to remove `//<![CDATA[` and end `//]]>`?

How to parse CDATA in XML in Groovy when xml elements are same

https://community.smartbear.com/t5/SoapUI-Open-Source/Groovy-Unexpected-element-CDATA/td-p/36454

Just trying to get past the "Unexpected element: CDATA" problem first, then I'll tackle the replace code a little more.

UPDATE I was asked to provide responseContent. Below is the idea of what the response is like, however I cannot provide the actual response due to PII and the response is thousands of lines of xml, wrapped in <![CDATA[ and ]]>.

<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>
      <GetActivitiesByQueryResponse xmlns="http://tempuri.org/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
         <GetActivitiesByQueryResult><![CDATA[<RECS SessionID="~~~~~~~~~~"><REC><Activity name="Activity" alias="Activity" keys="ActivityID"><ROW><ActivityID>~~~~~~~~~~~~~~~~~~~~~</ActivityID><ClientID>~~~~~~~~~~~~~~~~~~~~~~~~~~~</ClientID><ContactID>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</ContactID><WBS1>~~~~~~~~~</WBS1><WBS2>~~~~~~~~~</WBS2><WBS3>~~~~~~~~~~~</WBS3><Employee>~~~~~~~~~~~~</Employee><Type>~~~~~~~~~</Type><Subject>~~~~~~~~~~~~~~</Subject><StartDate>~~~~~~~~~~</StartDate><StartTime></StartTime><EndDate>~~~~~</EndDate><EndTime></EndTime><Duration>~~~~</Duration><Location>~~~~~~~</Location></ROW></ActivityCustomTabFields></REC></RECS>]]></GetActivitiesByQueryResult>
      </GetActivitiesByQueryResponse>
   </soap:Body>
</soap:Envelope>

I'd like to get the xml response without <![CDATA[ and ]]> (like below) so that I can extract the ActivityIDs with a groovy script.

<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>
      <GetActivitiesByQueryResponse xmlns="http://tempuri.org/~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~">
         <GetActivitiesByQueryResult><RECS SessionID="~~~~~~~~~~"><REC><Activity name="Activity" alias="Activity" keys="ActivityID"><ROW><ActivityID>~~~~~~~~~~~~~~~~~~~~~</ActivityID><ClientID>~~~~~~~~~~~~~~~~~~~~~~~~~~~</ClientID><ContactID>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</ContactID><WBS1>~~~~~~~~~</WBS1><WBS2>~~~~~~~~~</WBS2><WBS3>~~~~~~~~~~~</WBS3><Employee>~~~~~~~~~~~~</Employee><Type>~~~~~~~~~</Type><Subject>~~~~~~~~~~~~~~</Subject><StartDate>~~~~~~~~~~</StartDate><StartTime></StartTime><EndDate>~~~~~</EndDate><EndTime></EndTime><Duration>~~~~</Duration><Location>~~~~~~~</Location></ROW></ActivityCustomTabFields></REC></RECS></GetActivitiesByQueryResult>
      </GetActivitiesByQueryResponse>
   </soap:Body>
</soap:Envelope>

UPDATE While trying ou_ryperd's code, I got an error that had a lot to it but here is the start of it:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 43: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 43, column 262. 

This error got me thinking that somehow, maybe the response isn't in true XML and so that's why the CDATA was needed (I know XML only so well). Not sure how to work with this API however if it isn't XML... comments on this would be helpful.

Upvotes: 0

Views: 2541

Answers (1)

ou_ryperd
ou_ryperd

Reputation: 2133

Several issues here. I simplified your code to this, and it works:

    def s = """<![CDATA[<RECS SessionID="~~~~~~~~~~"><REC><Activity name="Activity" alias="Activity" keys="ActivityID"><ROW><ActivityID>~~~~~~~~~~~~~~~~~~~~~</ActivityID><ClientID>~~~~~~~~~~~~~~~~~~~~~~~~~~~</ClientID><ContactID>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</ContactID><WBS1>~~~~~~~~~</WBS1><WBS2>~~~~~~~~~</WBS2><WBS3>~~~~~~~~~~~</WBS3><Employee>~~~~~~~~~~~~</Employee><Type>~~~~~~~~~</Type><Subject>~~~~~~~~~~~~~~</Subject><StartDate>~~~~~~~~~~</StartDate><StartTime></StartTime><EndDate>~~~~~</EndDate><EndTime></EndTime><Duration>~~~~</Duration><Location>~~~~~~~</Location></ROW></ActivityCustomTabFields></REC></RECS>]]>"""
    s = s.replaceAll( "<!\\[CDATA\\[", "" ).replaceAll( "]]>", "" )
    log.info(s)

There are " characters in the response XML, which will break normal String operations. I used " " " and it worked for my example.

Additionally, in your example, you have Else which should be else.

Upvotes: 0

Related Questions