This package provides an object-model neutral API for the evaluation of XPath expressions and access to the evaluation environment.
The following XML standards apply:
The XPath language provides a simple, concise syntax for selecting nodes from an XML document. XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value. XPath is a W3C-defined language and an official W3C recommendation; the W3C hosts the XML Path Language (XPath) Version 1.0 specification.
XPath started in life in 1999 as a supplement to the XSLT and XPointer languages, but has more recently become popular as a stand-alone language, as a single XPath expression can be used to replace many lines of DOM API code.
An XPath expression is composed of a location path and one or more optional predicates. Expressions may also include XPath variables.
The following is an example of a simple XPath expression:
This example would select the
<bar> element in
an XML document such as the following:
<foo> <bar/> </foo>
/foo/bar is an example of a location
path. While XPath location paths resemble Unix-style file system
paths, an important distinction is that XPath expressions return
all nodes that match the expression. Thus, all three
<bar> elements in the following document would be
selected by the
<foo> <bar/> <bar/> <bar/> </foo>
A special location path operator,
//, selects nodes at
any depth in an XML document. The following example selects all
<bar> elements regardless of their location in a
A wildcard operator, *, causes all element nodes to be selected.
The following example selects all children elements of a
In addition to element nodes, XPath location paths may also address attribute nodes, text nodes, comment nodes, and processing instruction nodes. The following table gives examples of location paths for each of these node types:
||Selects the attribute
||Selects the text nodes of the
||Selects all comment nodes contained in the
||Selects all processing-instruction nodes contained in the
Predicates allow for refining the nodes selected by an XPath
location path. Predicates are of the form
[expression]. The following example selects all
<foo> elements that contain an
attribute with the value of
Predicates may be appended to each other to further refine an expression, such as:
The following example demonstrates using the XPath API to select one or more nodes from an XML document:
XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; InputSource inputSource = new InputSource("widgets.xml"); NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
While XPath expressions select nodes in the XML document, the XPath API allows the selected nodes to be coalesced into one of the following other data types:
The desired return type is specified by a
QName parameter in method call used to evaluate
the expression, which is either a call to
XPathExpression.evalute(...) or to one of the
XPath.evaluate(...) convenience methods. The allowed
QName values are specified as constants in the
XPathConstants class; they are:
Boolean return type is requested,
Boolean.TRUE is returned if one or more nodes were
Boolean.FALSE is returned.
String return type is a convenience for retrieving
the character data from a text node, attribute node, comment node, or
processing-instruction node. When used on an element node, the value
of the child text nodes is returned.
Number return type attempts to coalesce the text
of a node to a
double data type.
XPath location paths may be relative to a particular node in the
document, known as the
context. Consider the following
<widgets> <widget> <manufacturer/> <dimensions/> </widget> </widgets>
<widget> element can be selected with the
following XPath API code:
// parse the XML as a W3C Document DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE);
With a reference to the
<widget> element, a
relative XPath expression can now written to select the
<manufacturer> child element:
XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "manufacturer"; Node manufacturerNode = (Node) xpath.evaluate(expression, widgetNode, XPathConstants.NODE);
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2016, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.