My XML is:
<?xml version="1.0"?>
<company>
<staff id="1001" thedate="2013-01-01">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001" thedate="2014-01-01">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
I need a java code, which can compare dates from XML. I wrote this java code, but it does not work:
public static void main(String[] args) {
try {
File fXmlFile = new File("c:\\tmp\\test.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
NodeList nList = doc.getElementsByTagName("staff");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
String exp = "xs:date(@thedate) > xs:date('2013-05-01')"; //here comes the expression, but it does not work
System.out.println(selectBooleanValue(nNode,exp));
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static boolean selectBooleanValue(Node node, String xpathString) {
try {
XPath xpath = XPATH_FACTORY.get().newXPath();
XPathExpression expr = xpath.compile(xpathString);
boolean result = Boolean.valueOf(expr.evaluate(node, XPathConstants.BOOLEAN).toString());
return result;
} catch (XPathExpressionException e) {
return false;
}
}
private static ThreadLocal<XPathFactory> XPATH_FACTORY = new ThreadLocal<XPathFactory>() {
@Override
protected XPathFactory initialValue() {
return XPathFactory.newInstance();
};
};
I always get "false, false" output, so it does not work. The corresponding output would be: "false, true"
This line is the main question here:
String exp = "xs:date(@thedate) > xs:date('2013-05-01')"; //here comes the expression, but it does not work
I tried without xs:
but it is also does not work. I know there are other ways, but I need to solve this problem with that line! What kind of expression should I use to compare date values which are String variables actually?
I tried my code to compare strings, and it is works with it, but I need to compare dates!
Thank you!
XPath 2.0 which you seem to use has two greater than comparison operators, one is >
, the other is gt
. So try the expression xs:date(@thedate) > xs:date('2013-05-01')
or the expression xs:date(@thedate) gt xs:date('2013-05-01')
. >
is only used inside XSLT code to make sure the code is following XML rules, that is not something needed in pure XPath. But I wonder whether you use an XPath 2.0 implementation at all and whether you also do not need to set up a namespace resolver to map the xs
prefix to the XML schema namespace.
Assuming you have the Java built-in XPath 1.0 implementation all you can do is compare numbers so you could use number(translate(@thedate, '-', '')) > 20130501
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.