The Node Object


The Node object is the primary datatype in the whole model. It represents a single node in the XML document tree. There are different types of nodes, as enumerated in Table 16.1.

Table 16.1 NodeTypes

Type DOMCode

Element 1
Attribute 2
Text 3
CDATA Section 4
Entity Reference 5
Entity 6
Processing Instruction 7
Comment 8
Document 9
Document Type 10
Document Fragment 11
Notation 12

The Node object/interface provides you with generalized properties and methods for getting and setting node information. Other more specialized interfaces/objects (Element, Attribute, and so on) may contain additional and more convenient mechanisms for doing this.

Let's examine these general Node object properties first. See Table 16.2.



Table 16.2 Node Object Attributes/Properties

Name Value

nodeName Name (string) of the node (for example, tag, attributename)
nodeValue Value (string) of the node (for example, attributevalue, text)
nodeType The code representing the type of the object (1 for element, 2 for attribute, 3 for text, and so on for the other types)
parentNode The parent (node) of the given node
childNodes A NodeList object enumerating all children of this node
firstChild The first child (node) of a node
lastChild The last child (node) of a node
previousSibling The node immediately preceding the node
nextSibling The node immediately following the node
attributes A NamedNodeMap containing the attributes

Listing 16.1 is a JavaScript example using nodeName, nodeValue, and nodeType.

Listing 16.1 Using the nodeType, nodeValue, and nodeName Attributes of a Node Object


1:  //var node holds a Node Object
2:  if (node.nodeType == 2) //if an attribute
3:       return node.nodeValue;// return the attribute value
4:  if (node.nodeType == 1) // if an element
5:       return node.nodeName; // return the tag name

If a specific node in the tree is of type attribute, then the value (attributevalue) is returned.

If that node is of type element, then the tag name is returned.

Listing 16.2 shows another JavaScript example using context.

Listing 16.2 Using the previousSibling, nextSibling, and parentNode attributes of a Node Object


1:  //var node holds a Node Object
2:  var previous = node.previousSibling;
3:  var next = node.nextSibling;
4:  var parent = node.parentNode;
5:
6:  return "Hi, I'm " + node.nodeName + " .";
7:
8:  if (parent != null)
9:       return "My father is " + parent.nodeName + " .";
10:  if (previous != null)
11:       return "My older brother is " + previous.nodeName + " .";
12:  if (next != null)
13:       return "My younger brother is " + next.nodeName + " .";

The previous and next sibling and parent of a specific node are kept in 3 variables (lines 2-4). Then the name of the node is returned, followed by the name of the parent of the previous and the next sibling, if they exist.

Listing 16.3 is JavaScript example using the childNodes attribute of a Node object.

Listing 16.3 Using the childNodes Attribute of a Node Object


1:  //var node holds a Node Object
2:  var children = node.childNodes; //returns a NodeList (cf.infra)

Table 16.3 is an overview of the Node object methods.

Table 16.3 Node Object Methods

Name Returns

insertBefore(newChild,refChild) The node being inserted, where newChild is the node to insert and refChild is the node before which the node will be inserted
replaceChild(newChild,oldChild) The node being replaced, where newChild is the node that replaces the node oldChild
removeChild(oldChild) The node removed
appendChild(newChild) A node, where newChild is the node added to the end of the list of children
hasChildNodes() true or false, depending on whether the node has children or not
cloneNode(deep) A duplicate of the node. If the deep parameter is set to true, the cloning is done recursively for all the nodes in the subtree.

Listing 16.4 shows another JavaScript example.

Listing 16.4 Using the Methods of a Node Object


1:  //var node holding a Node Object
2:  //the node newElement has been created by the createElement method
3:  //of the Document Object (cf. infra)
4:  node.insertBefore(newElement,node.lastChild);
5:  //inserting the new element before the last child element
6:  node.replaceChild(newElement,node.firstChild);
7:  //replacing the first child element with the new element

In this piece of code we assume that we already have a variable named node holding a Node object and that we have created a new node named newElement using the createElement method of the Document object, to be discussed later.

Line 4 inserts the newElement node before the last child of the node with name node.

Line 6 replaces the first child element of node node with the newElement.