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

时间:2016-09-16 08:29:30

标签: soap groovy 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>

1 个答案:

答案 0 :(得分:4)

假设您的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'
]