Groovy web console

subscribe to the feed Subscribe
to this
site
parse and merge data conditionally (via #groovywebconsole)
tweet this snippet Tweet
this
script

parse and merge data conditionally

Published 2 weeks ago by nmrao with tags parse
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>
  <DataResponse xmlns="http://eh.dummy.com">
     <onlinePartyData xmlns="">
        <tabs xmlns="http://eh.dummy.com">
           <tabs_InnerSet>
              <Tabs_InnerSet_TupleType>
                 <tab>
                    <DISPLAY_TEXT>DESCRIPTION 1</DISPLAY_TEXT>
                    <qsets>
                       <qsets_InnerSet>
                          <Qsets_InnerSet_TupleType>
                             <qset>
                                <SET_CD>QS1</SET_CD>
                                <VISIBLE>true</VISIBLE>
                             </qset>
                          </Qsets_InnerSet_TupleType>
                          <Qsets_InnerSet_TupleType>
                             <qset>
                                <SET_CD>QS2</SET_CD>
                                <VISIBLE>true</VISIBLE>
                             </qset>
                             <qset>
                                <SET_CD>QS3</SET_CD>
                                <VISIBLE>false</VISIBLE>
                             </qset>
                          </Qsets_InnerSet_TupleType>

                       </qsets_InnerSet>
                    </qsets>
                 </tab>
              </Tabs_InnerSet_TupleType>
           </tabs_InnerSet>
        </tabs>
     </onlinePartyData>
  </DataResponse>
</soap:Body>
</soap:Envelope>
"""
def parsedXml = new XmlSlurper().parseText(xml)
def result = parsedXml.'**'.findAll {it.name() == 'qset'}.inject([]){li,element -> element.VISIBLE != true ?: li << element.SET_CD; li}.collect{it.text().trim()}.join(',')
println result
assert result instanceof String