Groovy web console

subscribe to the feed Subscribe
to this
site
search data (via #groovywebconsole)
tweet this snippet Tweet
this
script

search data

Published 4 months ago by nmrao with tags xml
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def response = """<s:Envelope xmlns:s="xxx" xmlns:a="xxx">
   <s:Header>
      <a:Action s:mustUnderstand="1">xxx</a:Action>
   </s:Header>
   <s:Body>
      <SearchResultsResponse xmlns="xxx">
          <SearchResultsResult xmlns:b="xxx" xmlns:i="xxx">
             <b:HolidaySearchResult>
                <b:FlightSearchResult>
                   <b:DepartureAirportName i:nil="true"/>
                   <b:InboundFlightInformation>
                      <b:FlightId>441070</b:FlightId>
                   </b:InboundFlightInformation>
                   <b:OutboundFlightInformation>
                      <b:FlightId>440941</b:FlightId>
                   </b:OutboundFlightInformation>
               </b:FlightSearchResult>
               <b:FlightSearchResult>
                   <b:DepartureAirportName i:nil="true"/>
                   <b:InboundFlightInformation>
                      <b:FlightId>441071</b:FlightId>
                   </b:InboundFlightInformation>
                   <b:OutboundFlightInformation>
                      <b:FlightId>440955</b:FlightId>
                   </b:OutboundFlightInformation>
               </b:FlightSearchResult>
               <b:FlightSearchResult>
                   <b:DepartureAirportName i:nil="true"/>
                   <b:InboundFlightInformation>
               <b:FlightId>441340</b:FlightId>
                   </b:InboundFlightInformation>
                   <b:OutboundFlightInformation>
               <b:FlightId>330941</b:FlightId>
                   </b:OutboundFlightInformation>
               </b:FlightSearchResult>
           </b:HolidaySearchResult>
       </SearchResultsResult>
   </SearchResultsResponse>
</s:Body>
</s:Envelope>"""
def xml = new XmlSlurper().parseText(response)

def getFlightIds = { type = '' ->
	type ? xml.'**'.findAll { it.name() == type }.collect { it.FlightId.text() } : xml.'**'.findAll {it.FlightId.text()}
}

println "Inbound flight ids: ${getFlightIds('InboundFlightInformation')}"
println "Outbound flight ids: ${getFlightIds('OutboundFlightInformation')}"
println "All flight ids: ${getFlightIds()}"