Saxonica.com

The NodeInfo Object

The NodeInfo object represents a node of an XML document. It has a subclass DocumentInfo to represent the root node, but all other nodes are represented by NodeInfo itself. These follow the XPath data model closely.

In earlier releases, NodeInfo extended the DOM interface Node. This is no longer the case; it has been changed to make it easier to integrate Saxon with other XML tree representations such as JDOM. Until Saxon 8.2, the main Saxon implementations of the NodeInfo interface continued to also implement the DOM Node interface, but this is no longer the case from Saxon 8.3 onwards: it has been changed because the DOM interface was becoming increasingly unwieldy, and in particular because it is incompatible between JDK 1.4 and JDK 1.5. Instead, a new class NodeOverNodeInfo is provided, which presents a DOM view of a Saxon node.

The NodeInfo object provides the application with information about the node. The most commonly used methods include:

getNodeKind()

gets a short identifying the node type (for example, element or attribute). The values are consistent with those used in the DOM, and are referenced by constants in the class net.sf.saxon.value.Type

getDisplayName(), getLocalPart(), getPrefix(), getURI()

These methods get the name of the element, or its various parts. The getDisplayName() method returns the QName as used in the original source XML.

getAttributeValue()

get the value of a specified attribute, as a String.

getStringValue()

get the string value of a node, as defined in the XPath data model

getTypedValue()

get the typed value of a node, as defined in the XPath 2.0 data model. This is in general a sequence of atomic values, so the result is a SequenceIterator.

getParent()

get the NodeInfo representing the parent element, (which will be a DocumentInfo object if this is the outermost element).

iterateAxis()

returns an SequenceIterator object that can be used to iterate over the nodes on any of the XPath axes. The first argument is an integer identifying the axis; the second is a NodeTest (a simple form of pattern) which can be used to filter the nodes on the axis. Supply null if you want all the nodes on the axis. (For most applications, it is probably simpler to navigate from a node by compiling an XPath expression, and executing it with the correct starting node as the context node).

For other methods, see the JavaDoc documentation.

It is possible (though not easy) to provide your own implementation of the NodeInfo interface, perhaps allowing Saxon queries to run directly against some non-XML data source. There are helper methods in the net.sf.saxon.om.Navigator class that reduce the amount of code you need to write to achieve this. See the implementations that map NodeInfo to DOM, DOM4J, JDOM or XOM to see how it is done.

Next