这是一个xml响应我试图用soapui脚本(groovy)断言来探索:
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<myInfoResponse xmlns="http://test.test.test.test">
<pc>1234223234</pc>
<item>
<sl>val1</sl>
<he>val2</he>
<ko>val3</ko>
<fo>val4</fo>
<ok>val5</ok>
<di>val6</di>
</item>
...
为什么我无法获得pc
节点的值:
def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.getNodeValue("/S:Envelope/S:Body/myInfoResponse/pc")
// Output: null
holder.getNodeValue("/S:Envelope/S:Body/myInfoResponse[1]/pc[1]")
// Output: null
我可以通过XPath获得价值
holder.getNodeValue("/S:Envelope/S:Body/*[1]/*[1]")
// Output: 1234223234
holder.getNodeValue("/S:Envelope/S:Body/*[1]/*[2]/*[4]")
// Output: val4
为什么?
答案 0 :(得分:1)
与评论中提到的一样,元素myInfoResponse
具有默认命名空间。这就是为什么你无法获得pc
的价值。
以下是使用getNodeValue
//Check if the response is not empty
assert context.response, 'Response is empty or null'
def holder = new com.eviware.soapui.support.XmlHolder(context.response)
//You may also change the prefix other than mentioned in the response like below
holder.declareNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/')
//Below namespace uri does not have prefix in the response, but now setting prefix as ns
holder.declareNamespace('ns', 'http://test.test.test.test')
def pcValue = holder.getNodeValue('//soap:Envelope/soap:Body/ns:myInfoResponse/ns:pc')
log.info "Response of has pc value : ${pcValue}"
使用 XmlSlurper :
assert context.response
def parsedXml = new XmlSlurper().parseText(context.response)
def pcValue = parsedXml.'**'.find {it.name() == 'pc'}.text()
log.info "Response of has pc value : ${pcValue}"
//Similarly you can find any element name, for example item/fo
def foVal = parsedXml.'**'.find {it.name() == 'fo'}.text()
log.info "fo value is : ${foVal}"
答案 1 :(得分:0)
仍然不知道如何让holder.getNodeValue()
正常工作,但我发现groovy中的XmlSlurper
工作正常。
def Envelope = new XmlSlurper().parseText(messageExchange.responseContentAsXml)
log.info("pc = " + Envelope.Body.myInfoResponse.pc.text())
// Output: 1234223234