Groovy web console

subscribe to the feed Subscribe
to this
site
parse xml and retrieve value (via #groovywebconsole)
tweet this snippet Tweet
this
script

parse xml and retrieve value

Published 6 months ago by nmrao with tags parse xml
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def xml = """<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soap:Body>   <Response xmlns="http://eh.dummy.com">      <DataReturn xmlns="">         <additionalData xsi:nil="true" xmlns="http://eh.dummy.com"/>         <message xmlns="http://eh.dummy.com">Success</message>         <returnCode xmlns="http://eh.dummy.com">0</returnCode>      </DataReturn>      <score xmlns="">410</score>      <sets xmlns="">         <sets_InnerSet xmlns="http://eh.dummy.com">            <visibleSetsInnerSet>               <set_cd>QS1</set_cd>            </visibleSetsInnerSet>            <visibleSetsInnerSet>               <set_cd>QS8</set_cd>            </visibleSetsInnerSet>            <visibleSetsInnerSet>               <set_cd>QS9</set_cd>            </visibleSetsInnerSet>         </sets_InnerSet>      </sets>      <responseCode xmlns="">success</responseCode>   </Response> </soap:Body> </soap:Envelope>"""
def parsedXml = new XmlSlurper().parseText(xml)
def result = parsedXml.'**'.findAll {it.name() == 'set_cd'}.collect{it.text().trim()}.join(',')
println result
assert result instanceof String