Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

XPath result while evaluating with XPathConstants.BOOLEAN

843834Dec 18 2008 — edited Jul 7 2009
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.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 4 2009
Added on Dec 18 2008
9 comments
548 views