2

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'
}
Rao
  • 20,781
  • 11
  • 57
  • 77

1 Answers1

1

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

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Thanks a lot! But i though that XmlSlurper has already unescape chars, because output of root.Body is shown correctly. – SlavondeR Rave May 05 '17 at 06:11
  • SlavondeR, No, if you look at source string, only part of the xml is unescaped and part is escaped that is where landing it problem. Hope it did what you are looking for. [Appreciate if you can accept it as answered](http://stackoverflow.com/help/someone-answers) – Rao May 05 '17 at 06:20
  • Rao, there is another problem, if you can help please. When i try parse soap within xml in it after unescape chars ive got error: [Fatal Error] :1:158: The processing instruction target matching "[xX][mM][lL]" is not allowed. How i can handle with it? Thanks <?xml version="1.0" encoding="UTF-8" – SlavondeR Rave May 05 '17 at 10:08
  • @SlavondeRRave, are you using soapui? may be you can create a new question with details as much as possible. or see [here](http://stackoverflow.com/questions/19889132/error-the-processing-instruction-target-matching-xxmmll-is-not-allowed) – Rao May 05 '17 at 10:17