0
votes

I'm using a groovy file where I used xmlParser to generate XML.Now, I want to get the tag values of the xml.

Here is my code

def rootnode = new XmlParser().parseText(responseXml);

Output

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
    <SOAP-ENV:Body><ns1:creditCardResponse xmlns:ns1="urn:creditCard">
        <return xsi:type="tns:RPResponse">
            <Status xsi:type="xsd:int">0</Status>

        </return>
    </ns1:creditCardResponse>
</SOAP-ENV:Body>

I have tried like rootnode.Status[0].text()

However its not getting. How can I get "Status" value in it? Little confused.

Thanks,

1
I have tried like rootnode.Status[0].text() - Shaik Nizam Ali

1 Answers

0
votes

Just use the "path" down to the var you are interested. You either have to "quote" the namespaces (that means, use strings as accessors, as chars like : and - would be interpreted by groovy) or use the groovy.xml.Namespace helper. E.g. (see comments):

def xml = new groovy.util.XmlParser().parseText('''\
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:creditCard">
    <SOAP-ENV:Body><ns1:creditCardResponse xmlns:ns1="urn:creditCard">
        <return xsi:type="tns:RPResponse">
            <Status xsi:type="xsd:int">666</Status>

        </return>
    </ns1:creditCardResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
''')

// XXX namespaces quoted
assert xml.'SOAP-ENV:Body'.'ns1:creditCardResponse'.return.Status.text()=='666'

// XXX access by namespace
def nsSoapEnv = new groovy.xml.Namespace('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV')
def nsNs1 = new groovy.xml.Namespace('urn:creditCard', 'ns1')
assert xml[nsSoapEnv.Body][nsNs1.creditCardResponse].return.Status.text()=='666'