[英]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=]
我试图用 groovy 做的是在
<errorInfo</errorInfo>
标签之间获取消息但结果我喜欢: java.util.regex.Matcher[pattern=<errorInfo>(.+?)</errorInfo> region=0,790 lastmatch=]
Can you help me to get response text with groovy你能帮我用 groovy 获取响应文本吗
You tagged this as XPath.您将其标记为 XPath。 In Groovy you should use
GPath
, which is very similar.在 Groovy 中,您应该使用
GPath
,它非常相似。 Never, never ever, use regular expressions with XML , it'll always bite you back.永远不要,永远不要在 XML 中使用正则表达式,它总是会反咬你一口。 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.
嵌套标签、编码、自闭合、空白处理、DTD 验证、实体解析、CDATA 部分、注释的相关性与否,以及至少命名空间,使得很难获得稳定的正则表达式成功。
You can get the node you are interested in with GPath as follows:您可以通过 GPath 获取您感兴趣的节点,如下所示:
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.或者, 这里有一种在 Groovy 中使用“真实”XPath 的方法。
You could simply go on with the xmlHolder.您可以简单地继续使用 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.