public abstract class Node
extends Object
The generic superclass for all the contents of an XML document. There are exactly eight kinds of nodes in XOM:
Element
Document
Text
Comment
Attribute
ProcessingInstruction
DocType
Namespace
Every instance of Node
is an
instance of one of these eight classes
(including, possibly, one of their subclasses).
Modifier and Type | Method and Description |
---|---|
abstract Node | copy()
Returns a deep copy of this node with no parent,
that can be added to the current document or a different one.
|
void | detach()
Removes this node from its parent so that it can be added
to a different parent node or document.
|
boolean | equals(Object o)
Tests for node identity.
|
String | getBaseURI()
Returns the base URI of this node as specified by
XML
Base, or the empty string if this is not known.
|
abstract Node | getChild(int position)
Returns the child of this node at the specified position.
|
abstract int | getChildCount()
Returns the number of children of this node.
|
Document | getDocument()
Returns the document that contains this node,
or null if this node is not currently part of a document.
|
ParentNode | getParent()
Returns the node that contains this node,
or null if this node does not have a parent.
|
abstract String | getValue()
Returns the XPath 1.0 string-value of this node.
|
int | hashCode()
Returns a unique identifier for this node.
|
Nodes | query(String xpath)
Returns the nodes selected by the XPath expression in the
context of this node in document order as defined by XSLT.
|
Nodes | query(String xpath,
XPathContext namespaces)
Returns the nodes selected by the XPath expression in the
context of this node in document order as defined in XSLT.
|
abstract String | toXML()
Returns the actual XML form of this node, such as might be
copied and pasted from the original document.
|
public abstract String getValue()
Returns the XPath 1.0 string-value of this node.
public final Document getDocument()
Returns the document that contains this node,
or null if this node is not currently part of a document.
Each node belongs to no more than one document at a time.
If this node is a Document
, then it returns
this node.
public String getBaseURI()
Returns the base URI of this node as specified by XML Base, or the empty string if this is not known. In most cases, this is the URL against which relative URLs in this node should be resolved.
The base URI of a non-parent node is the base URI of the
element containing the node. The base URI of a document
node is the URI from which the document was parsed,
or which was set by calling setBaseURI
on
on the document.
The base URI of an element is determined as follows:
xml:base
attribute,
then the value of that attribute is
converted from an IRI to a URI, absolutized if possible,
and returned.
xml:base
attribute, then the value of that attribute from the
nearest such ancestor is converted from an IRI to a URI,
absolutized if possible, and returned.
xml:base
attributes from other entities are
not considered.
setBaseURI()
has been invoked on
this element, then the URI most recently passed to that method
is absolutized if possible and returned.
setBaseURI()
, then the URI of that entity is
returned.
Absolutization takes place as specified by the XML Base specification. However, it is not always possible to absolutize a relative URI, in which case the empty string will be returned.
public final ParentNode getParent()
Returns the node that contains this node, or null if this node does not have a parent.
public void detach()
Removes this node from its parent so that it can be added to a different parent node or document. This method does nothing if the node does not have a parent.
XMLException
- if the parent refuses to detach this nodepublic abstract Node getChild(int position)
Returns the child of this node at the specified position.
position
- the index of the child node to returnIndexOutOfBoundsException
- if this node does not have childrenpublic abstract int getChildCount()
Returns the number of children of this node. This is always non-negative (greater than or equal to zero).
public abstract Node copy()
Returns a deep copy of this node with no parent, that can be added to the current document or a different one.
Per Bloch, the Cloneable
interface is just a mess and should
be avoided. However, I do not follow his suggestion of a copy
constructor exclusively because it is useful to be able to
copy a node without knowing its more specific type.
Ken Arnold agrees with this. It's more effective for
subclasses that can return an instance of the subclass.
public abstract String toXML()
Returns the actual XML form of this node, such as might be copied and pasted from the original document. However, this does not preserve semantically insignificant details such as white space inside tags or the use of empty-element tags vs. start-tag end-tag pairs.
public final boolean equals(Object o)
Tests for node identity. That is, two
Node
objects are equal
if and only if they are the same object.
equals
in class Object
o
- the object compared for equality to this nodeo
is this node; false otherwiseObject.equals(Object)
public final int hashCode()
Returns a unique identifier for this node.
The value returned is the same as returned by
super.hashCode()
because nodes use identity semantics.
hashCode
in class Object
Object.hashCode()
public final Nodes query(String xpath, XPathContext namespaces)
Returns the nodes selected by the XPath expression in the context of this node in document order as defined in XSLT. All namespace prefixes used in the expression should be bound to namespace URIs by the second argument.
Note that XPath expressions operate on the XPath data model,
not the XOM data model. XPath counts all adjacent
Text
objects as a single text node, and does not
consider empty Text
objects. For instance, an
element that has exactly three text children in XOM, will
have exactly one text child in XPath, whose value is the
concatenation of all three XOM Text
objects.
You can use XPath expressions that use the namespace axis. However, namespace nodes are never returned. If an XPath expression only selects namespace nodes, then this method will return an empty list.
No variables are bound.
The context position is the index of this node among its parents
children, counting adjacent text nodes as one. The context size
is the number of children this node's parent has, again counting
adjacent text nodes as one node. If the parent is a
Document
, then the DocType
(if any) is
not counted. If the node has no parent, then the context position
is 1, and the context size is 1.
Queries such as /*, //, and /*//p that refer to the
root node do work when operating with a context node that is not
part of a document. However, the query / (return the root node)
throws an XPathException
when applied to a node
that is not part of the document. Furthermore the top-level
node in the tree is treated as the first and only child of the
root node, not as the root node itself. For instance, this
query stores parent
in the result
variable, not child
:
Element parent = new Element("parent");
Element child = new Element("child");
parent.appendChild(child);
Nodes results = child.query("/*");
Node result = result.get(0);
xpath
- the XPath expression to evaluatenamespaces
- a collection of namespace prefix bindings
used in the XPath expressionXPathException
- if there's a syntax error in the
expression, the query returns something other than
a node-setpublic final Nodes query(String xpath)
Returns the nodes selected by the XPath expression in the context of this node in document order as defined by XSLT. This XPath expression must not contain any namespace prefixes.
No variables are bound. No namespace prefixes are bound.
xpath
- the XPath expression to evaluateXPathException
- if there's a syntax error in the
expression; or the query returns something other than
a node-setCopyright 2002-2023 Elliotte Rusty Harold
elharo@ibiblio.org