简体   繁体   中英

How to get the text value of an element in Groovy from within a soap envelope XML message

I have string like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <NS1:createResponse xmlns:NS1="http://abc.ru/esf/dto/srvMessages">
         <NS1:Header>
            <integrationID>wd457665grtyy5444</integrationID>
            <nativeID/>
            <resultInfo>
               <status>ERROR</status>
               <errorInfo>
                  <descr>Error:ESB-002: Failed to createSub. Code=Error whilst processing message:Fault from GW: faultcode=tns:Client faultstring=Could not map property BonusMalusRateForKSK detail=,,</descr>
               </errorInfo>
            </resultInfo>
         </NS1:Header>
      </NS1:createResponse>
   </soapenv:Body>
</soapenv:Envelope>

And I have code like:

MessageExchange[] me = myTestStepResult.getMessageExchanges()
            log.info "[ERROR] " + me[0].getResponseContent() 
            def matches = me[0].getResponseContent()  =~ '<errorInfo>(.+?)</errorInfo>'
            log.info "[Result" + matches

What I'm trying to do with groovy is to get message between <errorInfo</errorInfo> tag But as result I have like: java.util.regex.Matcher[pattern=<errorInfo>(.+?)</errorInfo> region=0,790 lastmatch=]

Can you help me to get response text with groovy

You tagged this as XPath. In Groovy you should use GPath , which is very similar. Never, never ever, use regular expressions with XML , it'll always bite you back. If not now, then later. The nested tags, encodings, self-closing, white-space handling, DTD validation, entity parsing, CDATA sections, relevance or not of comments, and not in the least, namespaces, make it very hard to get a stable regex that will always succeed.

You can get the node you are interested in with GPath as follows:

def bookId = response.'**'.find { book->
    book.author.text() == 'Lewis Carroll'
}.@id

Where ** stands for "depth first". Translated to your example it'll be something like this:

def matches = me[0].getResponseContent().'**'.find { node->
    node.name() == 'errorInfo'
}.descr.text()

Alternatively, here's a way to use "true" XPath within Groovy.

You could simply go on with the xmlHolder. Which is pretty simple

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder("RequestStepName#Response")
def nodeValue = holder.getNodeValue ("//*:errorInfo//*:descr")
log.info nodeValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM