-1

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

Abel
  • 56,041
  • 24
  • 146
  • 247
java_user
  • 929
  • 4
  • 16
  • 39

2 Answers2

1

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.

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247
  • It give me: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script15.groovy: 123: expecting '}', found '->' @ line 123, column 67. ntent().'**'.find { node->node->name() = ^ org.codehaus.groovy.syntax.SyntaxException: expecting '}', found '->' @ line 123, column 67. at – java_user Sep 11 '15 at 11:42
  • @java_user, sorry, it should've been `.`, not `->`, as your error says. Fixed, see update. – Abel Sep 11 '15 at 11:59
  • Says: groovy.lang.MissingPropertyException: No such property: ** for class: java.lang.String – java_user Sep 11 '15 at 12:05
  • @java_user, These methods are auto-imported. No, like `node-> node.name()...`. Have you [read that link](http://www.groovy-lang.org/processing-xml.html#_gpath)? I think your object may not yet be groovified, check groovy's [XmlParser](http://docs.groovy-lang.org/latest/html/api/groovy/util/XmlParser.html) for the appropriate method you need for your document. Is it java's `XmlDocument`, or SAX? – Abel Sep 11 '15 at 12:28
0

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
Eiston Dsouza
  • 394
  • 2
  • 13