【问题标题】:How to get the text value of an element in Groovy from within a soap envelope XML message如何从肥皂信封 XML 消息中获取 Groovy 中元素的文本值
【发布时间】:2015-09-11 10:34:49
【问题描述】:

我有这样的字符串:

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

我有这样的代码:

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

我想用 groovy 做的是在 &lt;errorInfo&lt;/errorInfo&gt; 标签之间获取消息 但结果我喜欢:java.util.regex.Matcher[pattern=&lt;errorInfo&gt;(.+?)&lt;/errorInfo&gt; region=0,790 lastmatch=]

你能帮我用 groovy 获取响应文本吗

【问题讨论】:

  • &lt;errorInfo&gt;\s*&lt;descr&gt;([^&lt;]+)&lt;/descr&gt;.
  • 你可以试试:\
  • 还是有这个错误:java.util.regex.Matcher[pattern= \

标签: java regex grails xpath groovy


【解决方案1】:

您将此标记为 XPath。在 Groovy you should use GPath 中,非常相似。 Never, never ever, use regular expressions with XML,它总会咬你一口。如果不是现在,那么以后。嵌套标签、编码、自闭合、空白处理、DTD 验证、实体解析、CDATA 部分、cmets 的相关性与否,以及至少命名空间,使得它非常难以获得一个始终成功的稳定正则表达式。

你可以通过GPath获取你感兴趣的节点,如下:

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

** 代表“深度优先”。翻译成你的例子,它会是这样的:

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

另外,here's a way to use "true" XPath 在 Groovy 中。

【讨论】:

  • 它给了我:org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:Script15.groovy:123:期待'}',找到'->'@第123行,第67列。ntent( ).'**'.find { node->node->name() = ^ org.codehaus.groovy.syntax.SyntaxException: 期待 '}',在第 123 行第 67 列找到'->'。在跨度>
  • @java_user,对不起,它应该是.,而不是-&gt;,正如你的错误所说。已修复,请参阅更新。
  • 说:groovy.lang.MissingPropertyException:没有这样的属性:** 对于类:java.lang.String
  • @java_user,这些方法是自动导入的。不,就像node-&gt; node.name()...。你有read that link吗?我认为您的对象可能还没有被 groovified,请检查 groovy 的 XmlParser 以了解您的文档所需的适当方法。是java的XmlDocument,还是SAX?
【解决方案2】:

您可以简单地继续使用 xmlHolder。这很简单

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多