FOM Reference > DOM Support > DOM > Node interface
  
Node interface
The Node interface is the primary datatype for the entire Document Object Model. It represents a single node in the document tree. While all objects implementing the Node interface expose methods for dealing with children, not all objects implementing the Node interface may have children. For example, Text nodes may not have children, and adding children to such nodes results in a DOMException being raised.
The attributes nodeName, nodeValue and attributes are included as a mechanism to get at node information without casting down to the specific derived interface. In cases where there is no obvious mapping of these attributes for a specific nodeType (e.g., nodeValue for an Element or attributes for a Comment), this returns null. Note that the specialized interfaces may contain additional and more convenient mechanisms to get and set the relevant information.
The values of nodeName, nodeValue, and attributes vary according to the node type as follows:
Interface
nodeName
nodeValue
attributes
Attr
name of attribute
value of attribute
null
CDATASection
#cdata-section
content of the CDATA Section
null
Comment
#comment
content of the comment
null
Document
#document
null
null
DocumentFragment
#document-fragment
null
null
DocumentType
document type name
null
null
Element
tag name
null
NamedNodeMap
Entity
entity name
null
null
EntityReference
name of entity referenced
null
null
Notation
notation name
null
null
ProcessingInstruction
target
entire content excluding the target
null
Text
#text
content of the text node
null
NodeType enumeration
An integer indicating which type of node this is.
* 
Numeric codes up to 200 are reserved to W3C for possible future use.
The NodeType enumeration has the following constants of type unsigned short.
ELEMENT_NODE = 1
The node is an Element.
ATTRIBUTE_NODE = 2
The node is an Attr.
TEXT_NODE = 3
The node is a Text node.
CDATA_SECTION_NODE = 4
The node is a CDATASection.
ENTITY_REFERENCE_NODE = 5
The node is an EntityReference.
ENTITY_NODE = 6
The node is an Entity.
PROCESSING_INSTRUCTION_NODE = 7
The node is a ProcessingInstruction.
COMMENT_NODE = 8
The node is a Comment.
DOCUMENT_NODE = 9
The node is a Document.
DOCUMENT_TYPE_NODE = 10
The node is a DocumentType.
DOCUMENT_FRAGMENT_NODE = 11
The node is a DocumentFragment.
NOTATION_NODE = 12
The node is a Notation.
DocumentPosition enumeration
A bitmask indicating the relative document position of a node with respect to another node.
If the two nodes being compared are the same node, then no flags are set on the return.
Otherwise, the order of two nodes is determined by looking for common containers -- containers which contain both. A node directly contains any child nodes. A node also directly contains any other nodes attached to it such as attributes contained in an element or entities and notations contained in a document type. Nodes contained in contained nodes are also contained, but less-directly as the number of intervening containers increases.
If there is no common container node, then the order is based upon order between the root container of each node that is in no container. In this case, the result is disconnected and implementation-specific. This result is stable as long as these outer-most containing nodes remain in memory and are not inserted into some other containing node. This would be the case when the nodes belong to different documents or fragments, and cloning the document or inserting a fragment might change the order.
If one of the nodes being compared contains the other node, then the container precedes the contained node, and reversely the contained node follows the container. For example, when comparing an element against its own attribute or child, the element node precedes its attribute node and its child node, which both follow it.
If neither of the previous cases apply, then there exists a most-direct container common to both nodes being compared. In this case, the order is determined based upon the two determining nodes directly contained in this most-direct common container that either are or contain the corresponding nodes being compared.
If these two determining nodes are both child nodes, then the natural DOM order of these determining nodes within the containing node is returned as the order of the corresponding nodes. This would be the case, for example, when comparing two child elements of the same element.
If one of the two determining nodes is a child node and the other is not, then the corresponding node of the child node follows the corresponding node of the non-child node. This would be the case, for example, when comparing an attribute of an element with a child element of the same element.
If neither of the two determining node is a child node and one determining node has a greater value of nodeType than the other, then the corresponding node precedes the other. This would be the case, for example, when comparing an entity of a document type against a notation of the same document type.
If neither of the two determining node is a child node and nodeType is the same for both determining nodes, then an implementation-dependent order between the determining nodes is returned. This order is stable as long as no nodes of the same nodeType are inserted into or removed from the direct container. This would be the case, for example, when comparing two attributes of the same element, and inserting or removing additional attributes might change the order between existing attributes.
The DocumentPosition enumeration has the following constants of type unsigned short.
DOCUMENT_POSITION_SAME = 0x00
The two nodes are the same.
DOCUMENT_POSITION_DISCONNECTED = 0x01
The two nodes are disconnected. Order between disconnected nodes is always implementation-specific.
DOCUMENT_POSITION_PRECEDING = 0x02
The node precedes the reference node.
DOCUMENT_POSITION_FOLLOWING = 0x04
The node follows the reference node.
DOCUMENT_POSITION_CONTAINS = 0x08
The node contains the reference node. A node which contains is always preceding, too.
DOCUMENT_POSITION_IS_CONTAINED = 0x10
The node is contained by the reference node. A node which is contained is always following, too.
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20
The determination of preceding versus following is implementation-specific.
attributes attribute
A NamedNodeMap containing the attributes of this node (if it is an Element) or null otherwise.
attributes
Access
read-only
Returns
NamedNodeMap
childNodes attribute
A NodeList that contains all children of this node. If there are no children, this is a NodeList containing no nodes.
childNodes
Access
read-only
Returns
NodeList
firstChild attribute
The first child of this node. If there is no such node, this returns null.
firstChild
Access
read-only
Returns
Node
lastChild attribute
The last child of this node. If there is no such node, this returns null.
lastChild
Access
read-only
Returns
Node
localName attribute
Returns the local part of the qualified name of this node.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.
localName
Access
read-only
Returns
String
namespaceURI attribute
The namespace URI of this node, or null if it is unspecified.
This is not a computed value that is the result of a namespace lookup based on an examination of the namespace declarations in scope. It is merely the namespace URI given at creation time.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null.
* 
Per the Namespaces in XML Specification [XML Namespaces] an attribute does not inherit its namespace from the element it is attached to. If an attribute is not explicitly given a namespace, it simply has no namespace.
namespaceURI
Access
read-only
Returns
String
nextSibling attribute
The node immediately following this node. If there is no such node, this returns null.
nextSibling
Access
read-only
Returns
Node
nodeName attribute
The name of this node, depending on its type; see the table above.
nodeName
Access
read-only
Returns
String
nodeType attribute
A code representing the type of the underlying object, as defined above.
nodeType
Access
read-only
Returns
unsigned short
nodeValue attribute
The value of this node, depending on its type; see the table above. When it is defined to be null, setting it has no effect. including if the node is read-only.
nodeValue
Access
read-write
Returns
String
Get throws
DOMException
DOMSTRING_SIZE_ERR: Raised when it would return more characters than fit in a DOMString variable on the implementation platform.
Set throws
DOMException
NO_MODIFICATION_ALLOWED_ERR: Raised when the node is readonly.
ownerDocument attribute
The Document object associated with this node. This is also the Document object used to create new nodes. When this node is a Document or a DocumentType which is not used with any Document yet, this is null.
ownerDocument
Access
read-only
Returns
Document
parentNode attribute
The parent of this node. All nodes, except Attr, Document, DocumentFragment, Entity, and Notation may have a parent. However, if a node has just been created and not yet added to the tree, or if it has been removed from the tree, this is null.
parentNode
Access
read-only
Returns
Node
prefix attribute
The namespace prefix of this node, or null if it is unspecified.
Note that setting this attribute, when permitted, changes the nodeName attribute, which holds the qualified name, as well as the tagName and name attributes of the Element and Attr interfaces, when applicable.
Note also that changing the prefix of an attribute that is known to have a default value, does not make a new attribute with the default value and the original prefix appear, since the namespaceURI and localName do not change.
For nodes of any type other than ELEMENT_NODE and ATTRIBUTE_NODE and nodes created with a DOM Level 1 method, such as createElement from the Document interface, this is always null .
prefix
Access
read-write
Returns
String
Set throws
DOMException
INVALID_CHARACTER_ERR: Raised if the specified prefix contains an illegal character.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
NAMESPACE_ERR: Raised if the specified prefix is malformed, if the namespaceURI of this node is null, if the specified prefix is "xml" and the namespaceURI of this node is different from the xml namespace, if this node is an attribute and the specified prefix is "xmlns" and the namespaceURI of this node is different from the xml namespace, or if this node is an attribute and the qualifiedName of this node is "xmlns" [XML Namespaces].
previousSibling attribute
The node immediately preceding this node. If there is no such node, this returns null.
previousSibling
Access
read-only
Returns
Node
appendChild method
Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
appendChildnewChild
Parameters
NodenewChild
The node to add.
If it is a DocumentFragment object, the entire contents of the document fragment are moved into the child list of this node
Returns
Node. The node added.
Throws
DOMException
HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to append is one of this node's ancestors.
WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
cloneNode method
Returns a duplicate of this node, i.e., serves as a generic copy constructor for nodes. The duplicate node has no parent; (parentNode is null.).
Cloning an Element copies all attributes and their values, including those generated by the XML processor to represent defaulted attributes, but this method does not copy any text it contains unless it is a deep clone, since the text is contained in a child Text node. Cloning an Attribute directly, as opposed to be cloned as part of an Element cloning operation, returns a specified attribute (specified is true ). Cloning any other type of node simply returns a copy of this node.
Note that cloning an immutable subtree results in a mutable copy, but the children of an EntityReference clone are readonly. In addition, clones of unspecified Attr nodes are specified. And, cloning Document, DocumentType, Entity, and Notation nodes is implementation dependent.
cloneNodedeep
Parameters
booleandeep
If true, recursively clone the subtree under the specified node; if false, clone only the node itself (and its attributes, if it is an Element).
Returns
Node. The duplicate node.
compareDocumentPosition method
Compares a node with this node with regard to their position in the document and according to the document order .
compareDocumentPositionother
Parameters
Nodeother
The node to compare against this node.
Returns
unsigned short. Returns how the given node is positioned relatively to this node.
Throws
DOMException
NOT_SUPPORTED_ERR: when the compared nodes are from different DOM implementations that do not coordinate to return consistent implementation-specific results.
hasAttributes method
Returns whether this node (if it is an element) has any attributes.
hasAttributes
Parameters
None
Returns
trueboolean. if this node has any attributes, false otherwise.
hasChildNodes method
Returns whether this node has any children.
hasChildNodes
Parameters
None
Returns
booleantrue if this node has any children, false otherwise.
insertBefore method
Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are inserted, in the same order, before refChild. If the newChild is already in the tree, it is first removed.
insertBeforenewChildrefChild
Parameters
NodenewChild
The node to insert.
NoderefChild
[optional] The reference node, i.e., the node before which the new node must be inserted.
Returns
Node. The node being inserted.
Throws
DOMException
HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors.
WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly or if the parent of the node being inserted is readonly.
NOT_FOUND_ERR: Raised if refChild is not a child of this node.
isSupported method
Tests whether the DOM implementation implements a specific feature and that feature is supported by this node.
isSupportedfeatureversion
Parameters
Stringfeature
The name of the feature to test. This is the same name which can be passed to the method hasFeature on DOMImplementation.
Stringversion
This is the version number of the feature to test. In Level 2, version 1, this is the string "2.0". If the version is not specified, supporting any version of the feature will cause the method to return true.
Returns
boolean. Returns true if the specified feature is supported on this node, false otherwise.
lookupNamespaceURI method
Look up the namespace URI associated to the given prefix, starting from this node.
lookupNamespaceURIprefix
Parameters
Stringprefix
The prefix to look for. If this parameter is null, the method will return the default namespace URI if any.
Returns
String. Returns the associated namespace URI or null if none is found.
normalize method
Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes. This can be used to ensure that the DOM view of a document is the same as if it were saved and re-loaded, and is useful when operations (such as XPointer [XPointer] lookups) that depend on a particular document tree structure are to be used.
* 
In cases where the document contains CDATASections, the normalize operation alone may not be sufficient, since XPointers do not differentiate between Text nodes and CDATASection nodes.
normalize
Parameters
None
Returns
void
removeChild method
Removes the child node indicated by oldChild from the list of children, and returns it.
removeChildoldChild
Parameters
NodeoldChild
The node being removed.
Returns
Node. The node removed.
Throws
DOMException
NO_MODIFICATION_ALLOWED_ERR: Raised if this node is readonly.
NOT_FOUND_ERR: Raised if oldChild is not a child of this node.
replaceChild method
Replaces the child node oldChild with newChild in the list of children, and returns the oldChild node.
If newChild is a DocumentFragment object, oldChild is replaced by all of the DocumentFragment children, which are inserted in the same order. If the newChild is already in the tree, it is first removed.
replaceChildnewChildoldChild
Parameters
NodenewChild
The new node to put in the child list.
NodeoldChild
The node being replaced in the list.
Returns
Node. The node replaced.
Throws
DOMException
HIERARCHY_REQUEST_ERR: Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to put in is one of this node's ancestors.
WRONG_DOCUMENT_ERR: Raised if newChild was created from a different document than the one that created this node.
NO_MODIFICATION_ALLOWED_ERR: Raised if this node or the parent of the new node is readonly.
NOT_FOUND_ERR: Raised if oldChild is not a child of this node.