需要一种方法来搜索带有最大日期+值==“ 001”的条目

如果我这样做是为了找到带有最大日期+值==“ 001”的条目,则它不起作用。有没有办法进行合并查询?

def xmls = new XmlSlurper().parse(new File("C:/file.xml"));
Object oTest = xmls.Test.Entry.find{ v -> v.Value == "001" }.max{ d ->
Date.parse('MM/dd/yyyy', d.Date.toString())


输入xml示例:

<Test>
<Entry>
   <Date>01/12/2017</Date>
   <Value>001</Value>
</Entry>
<Entry>
   <Date>02/15/2017</Date>
   <Value>001</Value>
</Entry>
<Entry>
   <Date>03/15/2017</Date>
   <Value>002</Value>
</Entry>
</Test>


我需要的输出是:

Date>02/15/2017</Date>
<Value>001</Value>

最佳答案

def xmls = new XmlSlurper().parseText('''<Test>
<Entry>
   <Date>01/12/2017</Date>
   <Value>001</Value>
</Entry>
<Entry>
   <Date>02/15/2017</Date>
   <Value>001</Value>
</Entry>
<Entry>
   <Date>03/15/2017</Date>
   <Value>002</Value>
</Entry>
</Test>''');

Object oTest = xmls.Entry.findAll{ v -> v.Value == "001" }.max{ d -> Date.parse('MM/dd/yyyy', d.Date.toString()) }
println groovy.xml.XmlUtil.serialize(oTest)


结果:

<?xml version="1.0" encoding="UTF-8"?>
<Entry>
  <Date>02/15/2017</Date>
  <Value>001</Value>
</Entry>

07-26 04:05