0

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.

Lauren
  • 13
  • 4

1 Answers1

0

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.

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23
  • Thank you @ou_ryperd for the helpful code! Unfortunately I get an error. Here is some of the very long error it gives: `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"` In my code, I was trying to capture the response in a variable, not copy and paste it into the groovy script. Are you aware of better code than what I have in the original question/post to assign the response of an endpoint to a variable? – Lauren Nov 19 '19 at 20:46
  • @Lauren try to access the cdata like this: `def responseContent = holder.response.responseContent def cdata = new XmlSlurper().parseText(responseContent).Body.GetActivitiesByQueryResponse.GetActivitiesByQueryResult.text() log.info(cdata) def cdataAsXML = new XmlSlurper().parseText(cdata) log.info(cdataAsXML.ROW.ActivityID)` At this point the CDATA as XML fails validation because it is not well-formed XML: `org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 578; The end-tag for element type "Activity" must end with a '>' delimiter. error at line: 37` I can't help you with that. – ou_ryperd Nov 20 '19 at 08:22
  • thank you, I got this: `groovy.lang.MissingPropertyException: No such property: holder for class: Script1 error at line: 1` and so I tried declaring holder by doing this: `def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) def holder = groovyUtils.getXmlHolder( "Request 1#Response" ) def responseContent = holder.response.responseContent ...` and got this: `Unexpected element: CDATA" at line 2` I believe I can't accomplish what I'm trying to do because The response is not true XML, which is probably why the developers made CDATA go around the response. – Lauren Nov 20 '19 at 15:45
  • Yeah, you could parse that data based on some delimiter, or some other clever way. Groovy is very powerful for that. At lease you can access that data now. – ou_ryperd Nov 21 '19 at 05:19