views:

568

answers:

1

I have used XmlSlurper successfully before, but am struggling to parse the following XML - it is a response from a call to the Pingdom API. I have tried following the namespace declaration examples, but I just get an error message on the ns1 and ns2 values. Can anybody help point me in the right direction? The xml looks like this:-

<?xml version="1.0" encoding="UTF-8"?>
    <SOAP-ENV:Envelope 
         xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
         xmlns:ns1="urn:methods" 
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:ns2="urn:PingdomAPI"
         xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
         SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
        <SOAP-ENV:Body>
            <ns1:Auth_loginResponse>
                <return xsi:type="ns2:Auth_LoginResponse">
                    <status xsi:type="xsd:int">0</status>
                    <sessionId xsi:type="xsd:string">mysessionId</sessionId>
                </return>
            </ns1:Auth_loginResponse>
        </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>

after using the XmlSlurper it just concatenates the 0 and mysessionId to one string

0mysessionId
+2  A: 

Try this, giving your xml is stored in the xml variable :

def records = new XmlSlurper().parseText(xml).declareNamespace(
    'SOAP-ENV':'http://schemas.xmlsoap.org/soap/envelope/', 
    ns1:'urn:methods', 
    xsd:'http://www.w3.org/2001/XMLSchema', 
    xsi:'http://www.w3.org/2001/XMLSchema-instance',
    ns2:'urn:PingdomAPI'
)

println records.'SOAP-ENV:Body'.'ns1:Auth_loginResponse'.return.status
println records.'SOAP-ENV:Body'.'ns1:Auth_loginResponse'.return.sessionId
Philippe