【问题标题】:Groovy - Parse SOAP response XML to get dataGroovy - 解析 SOAP 响应 XML 以获取数据
【发布时间】:2016-09-16 08:29:30
【问题描述】:

我有以下值作为文本。我需要解析 XML 并获取它们每个的值。请建议如何在 Groovy 中进行操作

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <ns0:GetListBy_QualificationResponse xmlns:ns0="urn:WS_CTM_People_ICEVA">
         <ns0:getListValues>
            <ns0:Person_ID>PPL000000301739</ns0:Person_ID>
            <ns0:Submitter>soehler</ns0:Submitter>
            <ns0:Profile_Status>Enabled</ns0:Profile_Status>
            <ns0:Locale2>en_US</ns0:Locale2>
            <ns0:VIP>No</ns0:VIP>
            <ns0:Client_Sensitivity>Standard</ns0:Client_Sensitivity>
         </ns0:getListValues>
      </ns0:GetListBy_QualificationResponse>
   </soapenv:Body>
</soapenv:Envelope>

【问题讨论】:

    标签: soap groovy xml-parsing


    【解决方案1】:

    假设你的 xml 在变量 xml 中的一个字符串中,那么你可以这样做:

    def mapOfValues = new XmlSlurper().parseText(xml)
                                      .Body
                                      .GetListBy_QualificationResponse
                                      .getListValues.children().collectEntries {
        [it.name(), it.text()]
    }
    

    这使得mapOfValues 等于包含以下内容的地图:

    [
        'Person_ID':'PPL000000301739',
        'Submitter':'soehler',
        'Profile_Status':'Enabled',
        'Locale2':'en_US',
        'VIP':'No',
        'Client_Sensitivity':'Standard'
    ]
    

    【讨论】:

    • @Nilotpol 这有帮助吗?你能用这个测试吗?我一直在使用相同的逻辑,但我只获取值而不是名称值对。
    猜你喜欢
    • 1970-01-01
    • 2020-01-29
    • 2021-06-07
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-07
    • 1970-01-01
    相关资源
    最近更新 更多