Groovy获取xml标签属性值

时间:2020-05-16 04:57:59

标签: groovy xml-parsing xmlslurper

我试图使用groovy获取属性 IntegratorCode ProductType 的值。

<?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>

我使用XmlSlurpur尝试了以下方法

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

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

我想打印IntegratorCode的值为“ GENERIC”。 我不确定在这种情况下我做错了什么。请帮忙。

1 个答案:

答案 0 :(得分:1)

您所说的结果 soap:Envelope ,因此,您实际上有了Envelope.Envelope ...这可以起作用:

Server.Transfer("Privacy");

作为预防措施,我通常按以下方式使用它:

println result.Header.Authentication.@IntegratorCode