使用 Groovy 在 SOAP 响应中将 <![CDATA[ 和 ]]> 替换为“”

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

我研究了这个问题,但是当整个 Soap XML 响应完全包含在 <![CDATA[]]> 中时,我找不到解决方案。

当我尝试将 <![CDATA[]]> 简单替换为 "" 时,我将 运行 保留在错误消息 "Unexpected element: CDATA" 中,我明白为什么但是我正在努力使用代码来绕过它。

这是我目前的情况:

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 )
   }

我正在使用 SoapUI 并访问了很多网站试图弄明白,这里有一些(很多让我不清楚 and/or 不适用于 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 `//]]>`?

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

首先尝试解决 "Unexpected element: CDATA" 问题,然后再解决替换代码问题。

更新 我被要求提供 responseContent。下面是响应的想法,但是由于 PII,我无法提供实际响应,并且响应是数千行 xml,包裹在 <![CDATA[]]> 中。

<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>

我想获得没有 <![CDATA[]]> 的 xml 响应(如下所示),以便我可以使用 groovy 脚本提取 ActivityID。

<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>

更新 在尝试 ou_ryperd 的代码时,我遇到了一个很重要的错误,但这是错误的开始:

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 "" @ line 43, column 262. 

这个错误让我想到,不知何故,也许响应不是真的 XML,所以这就是为什么需要 CDATA(我知道 XML 太好了)。不确定如何使用此 API 但是如果它不是 XML...对此的评论会有所帮助。

这里有几个问题。我将你的代码简化为这个,它有效:

    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)

响应中有 " 字符 XML,这会破坏正常的字符串操作。我使用了 " " ",它适用于我的示例。

另外,在你的例子中,你有 Else 应该是 else。