如何使用Groovy获取SOAPUI响应的属性值

发布于 2025-02-14 00:42:23 字数 1310 浏览 0 评论 0原文

我的肥皂响应:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的轨迹 2025-02-21 00:42:23

https://www.w3schools.com/xml/xml/xml/xpath_sytax_syntax.asp 在本文中

库存[1]是引用第一个元素的正确方法

- 另一个问题 -您正在尝试使用名称空间 “ // namf:namf )访问 element 当将默认名称空间重置为newdataset element在:&lt; newdataset xmlns =“”&gt;

因此,这应该有效

responseHolder.getNodeValue("//Inventory[1]/@attr1")

https://www.w3schools.com/xml/xpath_syntax.asp

check predicates section in this article

Inventory[1] is a right way to reference first element

another issue - you are trying to access Inventory element with a namespace "//namf:Inventory when default namespace was reset to empty on the level of NewDataSet element: <NewDataSet xmlns="">

So, this should work

responseHolder.getNodeValue("//Inventory[1]/@attr1")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文