需要一个 GPath 查询来查找最大日期 + 一个字符串字段

需要一种方法来搜索具有最大日期 + 值 ==“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>


Smart猫小萌
浏览 119回答 1
1回答

不负相思意

def xmls = new XmlSlurper().parseText('''<Test><Entry>&nbsp; &nbsp;<Date>01/12/2017</Date>&nbsp; &nbsp;<Value>001</Value></Entry><Entry>&nbsp; &nbsp;<Date>02/15/2017</Date>&nbsp; &nbsp;<Value>001</Value></Entry><Entry>&nbsp; &nbsp;<Date>03/15/2017</Date>&nbsp; &nbsp;<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>&nbsp; <Date>02/15/2017</Date>&nbsp; <Value>001</Value></Entry>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java