Peter Cook
Peter Cook

Reputation: 157

Collecting and formatting Groovy maps

I have another map question. This is is a matter of the format of the map and I can't seem to get it right. Here is the XML:

 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <SearchRS>
     <SearchStatus>SUCCESS</SearchStatus>
     <Itinerary>
         <Carrier>Joe</Carrier>
         <Total>111.11</Total>
         <Duration>111</Duration>
     </Itinerary>
     <Itinerary>
         <Carrier>Bob</Carrier>
         <Total>222.22</Total>
         <Duration>222</Duration>
     </Itinerary>
     <Itinerary>
         <Carrier>Joe</Carrier>
         <Total>333.33</Total>
         <Duration>333</Duration>
     </Itinerary>
     <Itinerary>
     <Itinerary>
          <Carrier>Bob</Carrier>
          <Total>444.44</Total>
          <Duration>444</Duration>
     </Itinerary>
          <Carrier>Joe</Carrier>
          <Total>234.10</Total>
          <Duration>167</Duration>
     </Itinerary>
 </SearchRS>

I want to create a simple map that would look like this:

[[carrier:Joe, cost:111.11, duration:111], [carrier:Bob, cost:222.22, duration:222], [carrier:Joe, cost:333.33, duration:333], [carrier:Bob, cost:444.44 duration:444], [carrier:Joe, cost:234.10, duration 167]]

What I'm seeing is this:

[[Joe:[111.11, 333.33, 234.10], Bob:[222.22, 444.44]]

This is my code such as it is;

 def carriers = [:]
 for (name in doc.'**'.findAll { it.name() == 'CarrierCode' }.unique()) {
        carriers[name] = doc.'**'.findAll { it.name() == 'Itinerary' && name == it.CarrierCode.text() }.collect {
        Double.parseDouble(it.DisplayTotal.text()) + Double.parseDouble(it.Duration.text())
      }
 }
 log.info("map is " + carriers)

I need help on these maps until I can get into a groovy class, I'm really stuck on these things and all help greatly appreciated!

Upvotes: 1

Views: 239

Answers (2)

daggett
daggett

Reputation: 28599

you want to build not just map but list of maps

def xml='''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <SearchRS>
     <SearchStatus>SUCCESS</SearchStatus>
     <Itinerary>
         <Carrier>Joe</Carrier>
         <Total>111.11</Total>
         <Duration>111</Duration>
     </Itinerary>
     <Itinerary>
         <Carrier>Bob</Carrier>
         <Total>222.22</Total>
         <Duration>222</Duration>
     </Itinerary>
     <Itinerary>
         <Carrier>Joe</Carrier>
         <Total>333.33</Total>
         <Duration>333</Duration>
     </Itinerary>
     <Itinerary>
     <Itinerary>
          <Carrier>Bob</Carrier>
          <Total>444.44</Total>
          <Duration>444</Duration>
     </Itinerary>
          <Carrier>Joe</Carrier>
          <Total>234.10</Total>
          <Duration>167</Duration>
     </Itinerary>
 </SearchRS>'''
def doc=new XmlSlurper().parseText(xml)
doc.Itinerary.collect{
    [
        carrier:  it.Carrier,
        cost:     it.Total.toBigDecimal(),
        duration: it.Total.toBigDecimal(),
    ]
}

Upvotes: 1

Bhanuchander Udhayakumar
Bhanuchander Udhayakumar

Reputation: 1621

Try with this code :

def root = new XmlSlurper().parse(new File ('/tmp/stack.xml'))

List <Map> totalresult = []

root.'**'.findAll{ it.name() == 'Itinerary'}.each{

    Map temp = [:]

    temp [it.Carrier.name()] = it.Carrier.text()

    temp [it.Total.name()] = it.Total.text()

    temp [it.Duration.name()] = it.Duration.text()

    totalresult << temp
}

println totalresult

Upvotes: 0

Related Questions