[英]Unable to access node values when inside a soap envelope using groovy dombuilder
Goal: I have a Soap Response message that I need to validate a specific element value. 目标:我有一条“肥皂响应”消息,我需要验证特定的元素值。 I want to use the Groovy DOMBuilder. 我想使用Groovy DOMBuilder。
If I have a Soap Response such as this: 如果我有这样的Soap Response:
def xmlResponse =
'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">\n' +
' <soap:Body>\n' +
' <ns1:getResponse xmlns:ns1="http://my.company.com">\n' +
' <return>value1</return>\n' +
'\t\t <return>value2</return>\n' +
' </ns1:getResponse>\n' +
' </soap:Body>\n' +
'</soap:Envelope>'
and I use the following code: 我使用以下代码:
def StringReader s = new StringReader(xmlResponse)
def xmldoc = groovy.xml.DOMBuilder.parse(s)
def elementName = xmldoc.documentElement
use (groovy.xml.dom.DOMCategory) {
println elementName.return.size()
println elementName.return[0].text()
}
elementName.return.size() is 0. If I remove the soap envelope and soap body of my response I get the correct answer of 2. I am not sure what I need to do such that I get the correct response with the right message. elementName.return.size()为0。如果删除响应的肥皂包和肥皂主体,我会得到正确的答案2。我不确定我需要做什么,才能得到正确的响应并带有正确的消息。
Thank you in advance if you can help! 预先感谢您的帮助!
It is easy/cheap to use XmlSlurper
instead: 改用XmlSlurper
既简单又便宜:
def slurped = new XmlSlurper().parseText( xmlResponse ).Body.getResponse.'return'
assert slurped.size() == 2
assert slurped[0].text() == 'value1'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.