dscl
dscl

Reputation: 1626

Parsing XML in Groovy

I've read through the documentation, but can't get this working. What I'm trying to do is parse this XML string

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>soapenv:Server</faultcode>
            <faultstring>TriggeredMessageFault</faultstring>
            <detail>
                <TriggeredMessageFault xmlns="urn:fault.domain.com">
                    <exceptionCode>INVALID_PARAMETER</exceptionCode>
                    <exceptionMessage>Invalid campaign object</exceptionMessage>
                </TriggeredMessageFault>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>

What I'm trying to do is get to exceptionCode and exceptionMessage and store those in two different variables.

Upvotes: 0

Views: 202

Answers (1)

dscl
dscl

Reputation: 1626

I actually figured out how to do this finally

def xmlstring = """<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    <soapenv:Body>
        <soapenv:Fault>
            <faultcode>soapenv:Server</faultcode>
            <faultstring>TriggeredMessageFault</faultstring>
            <detail>
                <TriggeredMessageFault xmlns="urn:fault.domain.com">
                    <exceptionCode>INVALID_PARAMETER</exceptionCode>
                    <exceptionMessage>Invalid campaign object</exceptionMessage>
                </TriggeredMessageFault>
            </detail>
        </soapenv:Fault>
    </soapenv:Body>
</soapenv:Envelope>"""

def soap_ns = new groovy.xml.Namespace("http://schemas.xmlsoap.org/soap/envelope/",'soapenv')
def root = new XmlParser().parseText(xmlstring)
println root[soap_ns.Body][soap_ns.Fault].detail.TriggeredMessageFault.exceptionMessage.text()

My issue was twofold. I didn't know that after loading it started at Envelope and I wasn't sure how to deal with the namespace.

Upvotes: 2

Related Questions