groovyxml-parsingxmlslurper

Groovy get xml tags attribute value


I am trying to get the value of the attribute IntegratorCode AND ProductType using groovy.

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:SOAPNYK1="http://www.w3.org/2001/XMLSchema"
    xmlns:SOAPNYK2="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:SOAPNYK3="http://schemas.xmlsoap.org/soap/encoding/">
    <soap:Header>
        <Authentication
            xmlns="urn:iQQ:API:22:iQQMessages.xsd" EmployeeCode="*****" IntegratorCode="GENERIC" Pwd="******" Usr="SYSUSER"/>
        </soap:Header>
        <soap:Body>
            <CanOfferProduct
                xmlns="urn:iQQ:API:22:iQQMessages.xsd">
                <OnQuotePackage
                    xmlns="" ProductType="GAP" QuoteNumber="74859">test
                </OnQuotePackage>/>
            </CanOfferProduct>
        </soap:Body>
    </soap:Envelope>

I tried the following method using XmlSlurpur

def result = new XmlSlurper().parseText(xml)

println(result.Envelope.Header[1].Authentication.@IntegratorCode)
//output = IntegratorCode

I wanted to print the value of IntegratorCode which is "GENERIC". I am not sure what I am doing wrong in this case. please help.


Solution

  • what you called result is soap:Envelope, so you essentially had Envelope.Envelope... This works:

    println result.Header.Authentication.@IntegratorCode
    

    As a precaution I normally use it like this:

    def Envelope = new XmlSlurper().parseText(xml)