I am trying to get the value of a boolean attribute but I am getting the wrong result when I try using XPathConstants.BOOLEAN.
When I use XPathConstants.STRING I do get the correct value.
The following code:
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" ?>" +
"<root check=\"false\" />";
InputSource inputSource = new InputSource(new StringReader(xml));
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputSource);
XPath xPath = XPathFactory.newInstance().newXPath();
System.out.println("check as string: " + xPath.evaluate("root/@check", document, XPathConstants.STRING));
System.out.println("check as boolean: " + xPath.evaluate("root/@check", document, XPathConstants.BOOLEAN));
}
Has the following output:
check as string: false
check as boolean: true
Please explain.