SlavondeR Rave
SlavondeR Rave

Reputation: 23

Unable to retrieve data while parsing soap message with escaped xml using groovy

I try to select value of node from soap message (with gt and lt symbols) , but cant do that, i can only get body (root.Body) and other nodes are not visible, it is empty result. What i'm making wrong? Thanks!

import groovy.util.slurpersupport.Node
import groovy.util.slurpersupport.NodeChild
import groovy.xml.XmlUtil


String source=
'''<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>
      &lt;ns0:GetListBy_QualificationResponse xmlns:ns0=&quot;urn:WS_CTM_People_ICEVA&quot;&gt;
         &lt;ns0:getListValues&gt;
            &lt;ns0:Person_ID&gt;PPL000000301739&lt;/ns0:Person_ID&gt;
            &lt;ns0:Submitter&gt;soehler&lt;/ns0:Submitter&gt;
            &lt;ns0:Profile_Status&gt;Enabled&lt;/ns0:Profile_Status&gt;
            &lt;ns0:Locale2&gt;en_US&lt;/ns0:Locale2&gt;
            &lt;ns0:VIP&gt;No&lt;/ns0:VIP&gt;
            &lt;ns0:Client_Sensitivity&gt;Standard&lt;/ns0:Client_Sensitivity&gt;
         &lt;/ns0:getListValues&gt;
      &lt;/ns0:GetListBy_QualificationResponse&gt;
   </soapenv:Body>
</soapenv:Envelope>'''

def root = new XmlSlurper().parseText(source)

def Submitter =root.Body.GetListBy_QualificationResponse.getListValues.'*'.find { node->
    node.name() == 'Submitter'
}

Upvotes: 2

Views: 236

Answers (1)

Rao
Rao

Reputation: 21379

It is because the xml is escaped. In order to be able to retrieve the data property, it is required to unescape the xml string and pass it XmlSlurper.

Here is how it can be done:

String source='''<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>
      &lt;ns0:GetListBy_QualificationResponse xmlns:ns0=&quot;urn:WS_CTM_People_ICEVA&quot;&gt;
         &lt;ns0:getListValues&gt;
            &lt;ns0:Person_ID&gt;PPL000000301739&lt;/ns0:Person_ID&gt;
            &lt;ns0:Submitter&gt;soehler&lt;/ns0:Submitter&gt;
            &lt;ns0:Profile_Status&gt;Enabled&lt;/ns0:Profile_Status&gt;
            &lt;ns0:Locale2&gt;en_US&lt;/ns0:Locale2&gt;
            &lt;ns0:VIP&gt;No&lt;/ns0:VIP&gt;
            &lt;ns0:Client_Sensitivity&gt;Standard&lt;/ns0:Client_Sensitivity&gt;
         &lt;/ns0:getListValues&gt;
      &lt;/ns0:GetListBy_QualificationResponse&gt;
   </soapenv:Body>
</soapenv:Envelope>'''

//map the unescape characters
def map = ['&lt;' : '<', '&gt;' : '>', '&quot;' : '"', '&apos;':'\'', '&amp;':'&']
//Replace them in source string
map.collect {k,v -> source = source.replaceAll(k,v)}
//Now parse it
def root = new XmlSlurper().parseText(source)
//Get the submitter
def submitter = root.'**'.find { it.name() == 'Submitter' }
println submitter

You can quickly try online Demo

Upvotes: 1

Related Questions