【问题标题】:Not able to fetch all XML tag values using groovy无法使用 groovy 获取所有 XML 标记值
【发布时间】: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 我得到的是空字符串

标签: xml groovy


【解决方案1】:

以下代码运行良好:

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>2312</ns:ProductId>   
         <ns:ProductName>2313</ns:ProductName>   
        </tem:objUserDetails></tem:getMotorPremium>
     </soapenv:Body>
</soapenv:Envelope>
"""
def ab = new XmlSlurper().parseText(response).Body.getMotorPremium.objUserDetails
println("Product code :"+ab.ProductCode.text())
println("Product Id :"+ab.ProductId.text())
println("Product Name :"+ab.ProductName.text())
ab.with {
    println("Product code :"+it.ProductCode.text())
    println("Product Id :"+it.ProductId.text())
    println("Product Name :"+it.ProductName.text())
}

也许这是logInfo 的问题?

【讨论】:

  • 我会再次检查并确认你
  • 知道了.. 抱歉有错别字.. 谢谢 Opal
猜你喜欢
  • 2013-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-07
  • 2018-03-20
  • 2018-10-30
  • 2018-05-06
  • 1970-01-01
相关资源
最近更新 更多