Groovy 从XML WAPI应答的两个不同级别编译数据
我有一个WAPI的回复,我必须通过它来获取两个不同层次的信息,并将它们放在一起 响应看起来是这样的(其中有更多的信息,但这就是我们所关心的):Groovy 从XML WAPI应答的两个不同级别编译数据,groovy,Groovy,我有一个WAPI的回复,我必须通过它来获取两个不同层次的信息,并将它们放在一起 响应看起来是这样的(其中有更多的信息,但这就是我们所关心的): 最便宜的价格,这很容易,但我完全失去了这一点,因为不同的水平。非常感谢您在这方面提供的任何帮助。我将您的XML放在这里文件/wapi.XML以下代码: class XmlWapiTest { static main(args) { File file = new File('files/wapi.xml') No
最便宜的价格,这很容易,但我完全失去了这一点,因为不同的水平。非常感谢您在这方面提供的任何帮助。我将您的XML放在这里
文件/wapi.XML
以下代码:
class XmlWapiTest {
static main(args) {
File file = new File('files/wapi.xml')
Node xml = new XmlParser().parse(file)
List<String> types = ['Student', 'Youth', 'Adult']
types.each { type ->
List typeList = xml.findAll { itinerary -> itinerary."EligibleTypes"."$type".text().toBoolean() }
int size = typeList.size()
def lowestMap = typeList.collectEntries { itinerary ->
[(itinerary."SellerCode".text().trim()): itinerary."DisplayPrice".text().toFloat()]
}.min { it.value }
println "${type.toUpperCase()}:$size:${lowestMap?.key}:${lowestMap?.value}"
}
}
}
编辑1:
根据您的意见,我们需要更改lowestMap
的零件:
def temporaryMap = [:]
typeList.each { itinerary ->
String key = itinerary."SellerCode".text().trim()
Float value = itinerary."DisplayPrice".text().toFloat()
if (temporaryMap.containsKey(key)) {
if (temporaryMap.get(key) > value) temporaryMap[key] = value
} else temporaryMap[key] = value
}
def lowestMap = temporaryMap.min { it.value }
这并不是给我最便宜的价格。在我的跑步中,我得到了这个:学生:196:WN:302.1其他类型与此相同。计数是正确的。以下是卖家代码的最便宜价格:[AA:160.34,NK:198.38,UA:285.30,AS:286.20,WN:289.80,B6:310.20,SY:418.80,0M:543.40]那么,这是否意味着卖家代码不是唯一的呢?我不清楚您的数据结构,如果SellerCode不是唯一的,那么我的代码将无法以最便宜的价格正常工作。每个行程节点都有一个唯一的SellerCode子节点,但可能有几十个甚至数百个行程节点。请检查我答案中的“编辑1”部分。
class XmlWapiTest {
static main(args) {
File file = new File('files/wapi.xml')
Node xml = new XmlParser().parse(file)
List<String> types = ['Student', 'Youth', 'Adult']
types.each { type ->
List typeList = xml.findAll { itinerary -> itinerary."EligibleTypes"."$type".text().toBoolean() }
int size = typeList.size()
def lowestMap = typeList.collectEntries { itinerary ->
[(itinerary."SellerCode".text().trim()): itinerary."DisplayPrice".text().toFloat()]
}.min { it.value }
println "${type.toUpperCase()}:$size:${lowestMap?.key}:${lowestMap?.value}"
}
}
}
STUDENT:2:AA:123.45
YOUTH:2:AA:123.45
ADULT:0:null:null
def temporaryMap = [:]
typeList.each { itinerary ->
String key = itinerary."SellerCode".text().trim()
Float value = itinerary."DisplayPrice".text().toFloat()
if (temporaryMap.containsKey(key)) {
if (temporaryMap.get(key) > value) temporaryMap[key] = value
} else temporaryMap[key] = value
}
def lowestMap = temporaryMap.min { it.value }