Xpath from xml java

Package javax.xml.xpath

Provides an object-model neutral API for the evaluation of XPath expressions and access to the evaluation environment.

1. XPath Overview

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.

2. XPath Expressions

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 element in an XML document such as the following:

The expression /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 elements in the following document would be selected by the /foo/bar expression:

A special location path operator, // , selects nodes at any depth in an XML document. The following example selects all elements regardless of their location in a document:

Читайте также:  What is java method server

A wildcard operator, *, causes all element nodes to be selected. The following example selects all children elements of a element:

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:

Examples of Location Path
Location Path Description
/foo/bar/@id Selects the attribute id of the element
/foo/bar/text() Selects the text nodes of the element. No distinction is made between escaped and non-escaped character data.
/foo/bar/comment() Selects all comment nodes contained in the element.
/foo/bar/processing-instruction() Selects all processing-instruction nodes contained in the element.

Predicates allow for refining the nodes selected by an XPath location path. Predicates are of the form [expression] . The following example selects all elements that contain an include attribute with the value of true :

Predicates may be appended to each other to further refine an expression, such as:

3. XPath Data Types

3.1 QName types

The 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 XPath.evaluate(. ) methods.

When a Boolean return type is requested, Boolean.TRUE is returned if one or more nodes were selected; otherwise, Boolean.FALSE is returned.

The 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.

The Number return type attempts to coalesce the text of a node to a double data type.

3.2 Class types

  • Boolean — Boolean.class
  • Number — Number.class
  • String — String.class
  • Nodeset — XPathNodes.class
  • Node — Node.class

Of the subtypes of Number , only Double, Integer and Long are supported.

3.3 Enum types

Enum types are defined in XPathEvaluationResult.XPathResultType that provide mappings between the QName and Class types above. The result of evaluating an expression using the XPathExpression.evaluateExpression(. ) or XPath.evaluateExpression(. ) methods will be of one of these types.

    NUMBER
    The Enum mapping for NUMBER supports Double, Integer and Long .

4. XPath Context

  • a node (the context node)
  • a pair of non-zero positive integers (the context position and the context size)
  • a set of variable bindings
  • a function library
  • the set of namespace declarations in scope for the expression

It is an XML document tree represented as a hierarchy of nodes, a Node for example, in the JDK implementation.

5. Using the XPath API

The element can be selected with the following process:

// parse the XML as a W3C Document DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new File("/widgets.xml")); //Get an XPath object and evaluate the expression XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; Node widgetNode = (Node) xpath.evaluate(expression, document, XPathConstants.NODE); //or using the evaluateExpression method Node widgetNode = xpath.evaluateExpression(expression, document, Node.class);

With a reference to the element, a relative XPath expression can be written to select the child element:

XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "manufacturer"; Node manufacturerNode = (Node) xpath.evaluate(expression, widgetNode, XPathConstants.NODE); //or using the evaluateExpression method Node manufacturerNode = xpath.evaluateExpression(expression, widgetNode, Node.class);

In the above example, the XML file is read into a DOM Document before being passed to the XPath API. The following code demonstrates the use of InputSource to leave it to the XPath implementation to process it:

XPath xpath = XPathFactory.newInstance().newXPath(); String expression = "/widgets/widget"; InputSource inputSource = new InputSource("widgets.xml"); NodeList nodes = (NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET); //or using the evaluateExpression method XPathNodes nodes = xpath.evaluateExpression(expression, inputSource, XPathNodes.class);

In the above cases, the type of the expected results are known. In case where the result type is unknown or any type, the XPathEvaluationResult may be used to determine the return type. The following code demonstrates the usage:

XPathEvaluationResult result = xpath.evaluateExpression(expression, document); switch (result.type())

The XPath 1.0 Number data type is defined as a double. However, the XPath specification also provides functions that returns Integer type. To facilitate such operations, the XPath API allows Integer and Long to be used in evaluateExpression method such as the following code:

int count = xpath.evaluateExpression("count(/widgets/widget)", document, Integer.class);

Источник

Оцените статью