如何使用Groovy获取SOAPUI响应的属性值
我的肥皂响应:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInventoriesResponse xmlns="http://xxx">
<GetInventoriesResult>
<NewDataSet xmlns="">
<Inventory attr1="1" attr2="101" />
<Inventory attr1="1" attr2="101" />
</NewDataSet>
</GetInventoriesResult>
</GetInventoriesResponse>
</soap:Body>
</soap:Envelope>
我想从//库存[0]中读取attr1的值[0]
我正在做的是
import groovy.sql.Sql
import java.sql.DriverManager
import java.sql.Connection
import javax.sql.DataSource
import java.sql.Driver;
import java.util.*;
import java.text.*;
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )
responseHolder.namespaces['ns4']='http://xxx'
def CounGetInventoriesResponse=responseHolder["count(//Inventory)"]
def CompanyId=responseHolder.getNodeValue("//namf:Inventory[0]/@attr1")
log.info "att1"+CompanyId
companyID的值显示为null
我如何解决此问题。这里缺少什么
my soap response:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetInventoriesResponse xmlns="http://xxx">
<GetInventoriesResult>
<NewDataSet xmlns="">
<Inventory attr1="1" attr2="101" />
<Inventory attr1="1" attr2="101" />
</NewDataSet>
</GetInventoriesResult>
</GetInventoriesResponse>
</soap:Body>
</soap:Envelope>
I would like to read the values of attr1 from //Inventory[0]
What I am doing is
import groovy.sql.Sql
import java.sql.DriverManager
import java.sql.Connection
import javax.sql.DataSource
import java.sql.Driver;
import java.util.*;
import java.text.*;
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )
responseHolder.namespaces['ns4']='http://xxx'
def CounGetInventoriesResponse=responseHolder["count(//Inventory)"]
def CompanyId=responseHolder.getNodeValue("//namf:Inventory[0]/@attr1")
log.info "att1"+CompanyId
value of companyid is displayed as null
How do I resolve this . What is missing here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
https://www.w3schools.com/xml/xml/xml/xpath_sytax_syntax.asp 在本文中
库存[1]
是引用第一个元素的正确方法- 另一个问题 -您正在尝试使用名称空间
“ // namf:namf )访问 element 当将默认名称空间重置为
newdataset
element在:&lt; newdataset xmlns =“”&gt;
因此,这应该有效
https://www.w3schools.com/xml/xpath_syntax.asp
check predicates section in this article
Inventory[1]
is a right way to reference first elementanother issue - you are trying to access
Inventory
element with a namespace"//namf:Inventory
when default namespace was reset to empty on the level ofNewDataSet
element:<NewDataSet xmlns="">
So, this should work