【问题标题】:how to get attribute value for soapui response using groovy如何使用groovy获取soapui响应的属性值
【发布时间】:2022-07-08 18:30:39
【问题描述】:

我的肥皂回应:

<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>

我想从 //Inventory[0] 读取 attr1 的值

我正在做的是

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

我该如何解决这个问题。这里缺少什么

【问题讨论】:

  • 我在您的 xml 中没有看到 CompanyId 属性
  • 更新了xml
  • 你的问题是关于xpath。您的 groovy 代码没有问题。最好删除 groovy 标签并添加 xmlxpath 标签 - 这样您可以获得更多关于您的实际问题的答案。

标签: groovy soapui


【解决方案1】:

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

检查本文中的谓词部分

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

另一个问题 - 当默认命名空间在 NewDataSet 元素的级别上重置为空时,您正尝试使用命名空间 "//namf:Inventory 访问 Inventory 元素:&lt;NewDataSet xmlns=""&gt;

所以,这应该工作

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多