Nilotpol Nilotpol - 2 months ago 31
Groovy Question

Groovy - Parse SOAP response XML to get data

I have the below value as text. I need to parse the XML and get the value for each of them. Please suggest how to do in 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>

Answer

Assuming your xml is in a String in the variable xml, then you can do:

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

Which makes mapOfValues equal to a Map containing:

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