【发布时间】:2015-07-17 07:13:21
【问题描述】:
我正在尝试获取一个属性的 xml 节点。但它没有正确获取
这是我的回复
def response = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:ns="http://schemas.datacontract.org/2004/07/" xmlns:arr="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<soapenv:Header/>
<soapenv:Body>
<tem:getMotorPremium><tem:objUserDetails>
<ns:ProductCode>2311</ns:ProductCode>
<ns:ProductId>2311</ns:ProductId>
<ns:ProductName>2311</ns:ProductName>
</tem:objUserDetails></tem:getMotorPremium>
</soapenv:Body>
</soapenv:Envelope>
我通过以下方式获取每个值。
def code = new XmlSlurper().parseText(response)
.Body
.getMotorPremium
.objUserDetails
.ProductCode
.text()
def Id = new XmlSlurper().parseText(response)
.Body
.getMotorPremium
.objUserDetails
.ProductId
.text()
def Name = new XmlSlurper().parseText(response)
.Body
.getMotorPremium
.objUserDetails
.ProductName
.text()
我不想一直使用“new XmlSlurper().parseText(response).Body.getMotorPremium.objUserDetails”
我用过类似的东西来尝试,但它不起作用..请指教
def ab = new XmlSlurper().parseText(response).Body.getMotorPremium.objUserDetails
logInfo("Product code :"+ab.ProductCode.text());
logInfo("Product Id :"+ab.ProductId.text());
logInfo("Product Name :"+ab.ProductName.text());
【问题讨论】:
-
您提供的代码运行良好。您使用的是哪个版本的 groovy?
-
@Opal 我得到的是空字符串