【问题标题】:Groovy get xml tags attribute valueGroovy 获取 xml 标签属性值
【发布时间】:2020-05-16 04:57:59
【问题描述】:

我正在尝试使用 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”。 我不确定在这种情况下我做错了什么。请帮忙。

【问题讨论】:

    标签: groovy xml-parsing xmlslurper


    【解决方案1】:

    你所谓的 result 是 soap:Envelope,所以你基本上有 Envelope.Envelope... 这行得通:

    println result.Header.Authentication.@IntegratorCode
    

    作为预防措施,我通常这样使用它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多