我有另一张地图问题。这是地图格式的问题,我似乎无法做到正确。这是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>
我想创建一个看起来像这样的简单地图:
[[carrier:Joe,cost:111.11,duration:111],[carrier:Bob,cost:222.22,duration:222],[carrier:Joe,cost:333.33,duration:333],[carrier:Bob] ,费用:444.44持续时间:444],[承运人:乔,费用:234.10,工期167]]
我所看到的是:
[[Joe:[111.11,333.33,234.10],Bob:[222.22,444.44]]
这是我的代码,例如它;
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)
我需要这些地图上的帮助,直到我能进入一个时髦的课程,我真的坚持这些事情,所有人都非常感谢!
答案 0 :(得分:1)
您不仅要构建地图,还要构建地图列表
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(),
]
}
答案 1 :(得分:0)
尝试使用此代码:
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