简体   繁体   English

无法使用Groovy获取所有XML标签值

[英]Not able to fetch all XML tag values using groovy

I'm trying to fetch xml node of one attribute. 我正在尝试获取一个属性的xml节点。 But its not fetching properly 但是它不能正确获取

here is my response 这是我的回应

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>  

I'm getting values each in the following way. 我通过以下方式获取每个值。

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

I dont want to use "new XmlSlurper().parseText(response).Body.getMotorPremium.objUserDetails" all the time 我不想一直使用“ new XmlSlurper()。parseText(response).Body.getMotorPremium.objUserDetails”

I've used something like this to try, but its not working.. Kindly advice 我已经尝试过类似的方法,但是无法正常工作。

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());

The following code works perfectly fine: 以下代码可以正常工作:

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())
}

Maybe this is logInfo problem? 也许这是logInfo问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM