gpt4 book ai didi

java - 在java中比较日期与Xpath

转载 作者:行者123 更新时间:2023-11-29 04:45:02 25 4
gpt4 key购买 nike

我有一个使用 Java 中的 DOM 解析的 XML 页面。当我使用 XPath 执行查询时,例如 price <10price >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>

这是我的 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");
}
}
}

我需要做的是比较publish_date到预定日期。"//书[publish_date>2000-01-01]/*像这样的东西

最佳答案

Xpath(至少 1.0)无法比较日期,但您可以使用转换功能将日期按正确的顺序转换为整数:

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

关于java - 在java中比较日期与Xpath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37533570/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com