问题描述
我有简单的模拟服务,请求如下:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:fa1="http://TargetNamespace.com/FA1">
<soapenv:Header/>
<soapenv:Body>
<fa1:items>
<fa1:itemname>logo</fa1:itemname>
<fa1:price></fa1:price>
<fa1:vat>10</fa1:vat>
<fa1:description>nové logo</fa1:description>
</fa1:items>
</soapenv:Body>
</soapenv:Envelope>
我已经使用脚本来控制响应值。 开始时,XmlHolder无效。所以我用
def req = mockRequest.getContentElement().execQuery("/")
def records = new XmlParser(false,false).parseText(req[0].xmlText())
获取请求并将其解析为节点。
{http://TargetNamespace.com/FA1}items[attributes={}; value=[
{http://TargetNamespace.com/FA1}itemname[attributes={}; value=[logo]],{http://TargetNamespace.com/FA1}price[attributes={}; value=[1]],{http://TargetNamespace.com/FA1}vat[attributes={}; value=[10]],{http://TargetNamespace.com/FA1}description[attributes={}; value=[nové logo]]]
]
但是我无法获得价格值。我尝试过
def price = records.price.text()
表达式records.find( { it.text() == "1"} )
返回{http://TargetNamespace.com/FA1}price[attributes={}; value=[1]]
但是records.find( { it.name() == "price"} )
和records.find( { it.name() == "{http://TargetNamespace.com/FA1}price"} )
一样都返回null。
解决方法
关键是使用
def records = new XmlParser(
假,假 ).parseText(req[0].xmlText())
这样带来的请求值:
fa1:items[attributes={xmlns:soapenv=http://schemas.xmlsoap.org/soap/envelope/,xmlns:fa1=http://TargetNamespace.com/FA1}; value=[
fa1:itemname[attributes={}; value=[logo]],fa1:price[attributes={}; value=[1]],fa1:vat[attributes={}; value=[10]],fa1:description[attributes={}; value=[nové logo]]]
]
然后您可以按名称namespace:name
读取值,因此
records.find( { it.name() == "fa1:price"} ).text()
返回 1