commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Preissler, Z 1903, BN" <G.Preiss...@DeutschePost.de>
Subject JXPath: evaluating XPath against DOM Element?
Date Thu, 08 Dec 2005 13:20:12 GMT
Is there any way to use JXPath to evaluate XPath statements relative to a
DOM element node? In the following code fragment
everything works as expected if I create a JXPathContext with an
org.w3c.dom.Document as contextBean, but if I use the document's
root element, it returns <null> whenever I try to evaluate an XPath
expression.

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(is);
        JXPathContext docContext = JXPathContext.newContext(doc);
        docContext.setLenient(true);
        //this works
        Object val = null;
        Pointer ptr = docContext.createPath("/address");
        JXPathContext childContext = docContext.getRelativeContext(ptr);
        val = childContext.getValue("street");
        // val == "Orchard Road"
        //this doesn't
        Element root = doc.getDocumentElement();
        JXPathContext elementContext = JXPathContext.newContext(root);
        elementContext.setLenient(true);
        Object val2 = elementContext.getValue("/address/street");
        // val2 == null

Since I'd like to use an Element burried inside a larger document as
contextBean, first creating the childContext and removing any
leading slashes from xpath strings is a little cumbersome. Is there a better
way?

TIA
	Jerry

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message