Groovy - 解析 SOAP 响应 XML 以获取数据

Nil*_*pol 2 groovy soap xml-parsing

我有以下值作为文本。我需要解析 XML 并获取每个 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>
Run Code Online (Sandbox Code Playgroud)

tim*_*tes 5

假设您的 xml 在变量中的字符串中xml,那么您可以执行以下操作:

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

这使得mapOfValues等于 Map 包含:

[
    'Person_ID':'PPL000000301739',
    'Submitter':'soehler',
    'Profile_Status':'Enabled',
    'Locale2':'en_US',
    'VIP':'No',
    'Client_Sensitivity':'Standard'
]
Run Code Online (Sandbox Code Playgroud)