java中日期与Xpath的比较

java中日期与Xpath的比较,java,date,xpath,xml-parsing,date-comparison,Java,Date,Xpath,Xml Parsing,Date Comparison,我有一个在Java中使用DOM解析的XML页面。当我使用XPath执行查询时,例如price 20,我得到了预期的结果。然而,当我尝试按日期进行比较时,我无法得到任何结果。NetBeans说它很成功,但没有给我任何结果 此代码来自XML页面: <!?xml version="1.0" encoding="UTF-8"?><catalog ><book id="bk101"><author>Gambardella, Matthew</autho

我有一个在Java中使用DOM解析的XML页面。当我使用XPath执行查询时,例如
price 20
,我得到了预期的结果。然而,当我尝试按日期进行比较时,我无法得到任何结果。NetBeans说它很成功,但没有给我任何结果

此代码来自XML页面:

<!?xml version="1.0" encoding="UTF-8"?><catalog ><book id="bk101"><author>Gambardella, Matthew</author><title>XML Developer's Guide</title><genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications 
with XML.</description>
</book>
<catalog>
Gambardella,MatthewXML开发人员指南计算机
44.95
2000-10-01
深入了解如何创建应用程序
使用XML。
这是我的Java代码:

public class Main {
    public static void main(String[] args) throws XPathExpressionException, FileNotFoundException {

        XPathFactory factory = XPathFactory.newInstance();
        XPath path = factory.newXPath();
        XPathExpression xPathExpression=path.compile("//book[price >10]/* ");
        //| //book[price>10]/*

        File xmlDocument =new File("books.xml");
        InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));

        Object result = xPathExpression.evaluate(inputSource,XPathConstants.NODESET);

        NodeList nodeList = (NodeList)result;

        for (int i = 0; i < nodeList.getLength(); i++) {
            System.out.print(nodeList.item(i).getNodeName()+" ");
            System.out.print(nodeList.item(i).getFirstChild().getNodeValue());
            System.out.print("\n");
        }
    }
}
公共类主{
公共静态void main(字符串[]args)抛出XPathExpressionException、FileNotFoundException{
XPathFactory=XPathFactory.newInstance();
XPath path=factory.newXPath();
XPathExpression=path.compile(//book[price>10]/*”;
//|//图书[价格>10]/*
文件xmlDocument=新文件(“books.xml”);
InputSource InputSource=新的InputSource(新文件InputStream(xmlDocument));
对象结果=xPathExpression.evaluate(inputSource,XPathConstants.NODESET);
NodeList NodeList=(NodeList)结果;
for(int i=0;i
我需要做的是将
publish\u date
与预定义的日期进行比较。 “//图书[出版日期>2000-01-01]/* 类似这样的东西Xpath(至少1.0)无法比较日期,但您可以使用translate函数将日期转换为顺序正确的整数:

//book[translate(publish_date,'-','') > 20000101]

这就解决了我的问题,非常感谢你,很抱歉没有及时回复!很高兴能帮上忙。祝你好运!