Java in a Nutshell, 5th Edition [Electronic resources]

نسخه متنی -صفحه : 1191/ 1084
نمايش فراداده

XPathjavax.xml.xpath

Java 5.0

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 the expression evaluation as a String. The three-argument versions expect a third argument that specifies the desired return type and return an Object of an appropriate type. The valid types are the QName objects defined in the XPathConstants class, such as XPathConstants.NODE and XPathConstants.NODESET. With the DOM object model, evaluate( ) returns org.w3c.dom.Node and org.w3c.dom.NodeList objects for these types.

public interface

XPath { // Public Instance Methods XPathExpression

compile (String

expression ) throws XPathExpressionException; String

evaluate (String

expression , Object

item ) throws XPathExpressionException; String

evaluate (String

expression , org.xml.sax.InputSource

source ) throws XPathExpressionException; Object

evaluate (String

expression , org.xml.sax.InputSource

source , javax.xml.namespace.QName

returnType ) throws XPathExpressionException; Object

evaluate (String

expression , Object

item , javax.xml.namespace. QName

returnType ) throws XPathExpressionException; javax.xml.namespace.NamespaceContext

getNamespaceContext ( ); XPathFunctionResolver

getXPathFunctionResolver ( ); XPathVariableResolver

getXPathVariableResolver ( ); void

reset ( ); void

setNamespaceContext (javax.xml.namespace.NamespaceContext

nsContext ); void

setXPathFunctionResolver (XPathFunctionResolver

resolver ); void

setXPathVariableResolver (XPathVariableResolver

resolver ); }

Returned By

XPathFactory.newXPath( )