An XPath object is used to compile or evaluate an XPath expression. Create an XPath object through an XPathFactory. Configuration methods of XPath allow you to specify an XPathVariableResolver and an XPathFunctionResolver to resolve variable and function references in XPath expressions. You may also specify the javax.xml.namespace.NamespaceContext with which the XPath can resolve qualified names.

After creating and configuring an XPath object, you can use the compile( ) method to compile an XPath expression for later evaluation, or you can use one of the evaluate( ) methods to compile and evaluate an expression directly. There are four versions of evaluate( ). All expect a String containing an XPath expression as their first argument. The second argument is the document or portion of a document to evaluate the expression against. Two versions of evaluate( ) expect an org.xml.sax.InputSource for this second argument. These versions of the method first parse the document and build a DOM (or other object model) tree. The other two versions of evaluate( ) expect an Object as the second argument. The object passed should be a DOM (or other object model) object representing the document or some portion of it. For the org.w3c.dom object model, this might be a Document, DocumentFragment, Node, or NodeList object.

The final difference between evaluate( ) methods is the presence or absence of a third argument. The two-argument versions of evaluate( ) return the result of ...

Get Java in a Nutshell, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.