XML DOM » Objects » Node

The Node object is the basic datatype of the whole Document Object Model, and represents a single node in the document tree. Note that not all node types may have children even though methods to deal with them are available. For a list of all node types and any child node types they may have, see the DOM Structure Model.

Node Types
Below is a list of the valid node types and their enumeration. It is this number that is returned by the nodeType property.

  • NODE_ELEMENT(1)
    This node represents an element.
  • NODE_ATTRIBUTE(2)
    This node represents an attribute of an element. Note that it is not considered a child of the element node.
  • NODE_TEXT(3)
    This node represents the text content of a tag.
  • NODE_CDATA_SECTION(4)
    This node represents the CDATA section of the XML source. CDATA sections are used to escape blocks of text that would otherwise be considered as markup.
  • NODE_ENTITY_REFERENCE(5)
    This node represents a reference to an entity in the XML document.
  • NODE_ENTITY(6)
    This node represents an expanded entity.
  • NODE_PROCESSING_INSTRUCTION(7)
    This node represents a processing instruction from the XML document.
  • NODE_COMMENT(8)
    This node represents a comment in the XML document.
  • NODE_DOCUMENT(9)
    This node represents an XML document object.
  • NODE_DOCUMENT_TYPE(10)
    This node represents the document type declaration of the <!DOCTYPE> tag.
  • NODE_DOCUMENT_FRAGMENT(11)
    This node represents a document fragment. This associates a node or subtree with a document without actually being part of it.
  • NODE_NOTATION(12)
    This node represents a notation in the document type declaration.
The values of nodeName and nodeValue vary according to the type of node. These values are detailed in the table below.

Type nodeName nodeValue
Element tagName null
Attr name of attribute value of attribute
Text #text content of text node
CDATASection #cdata-section content of CDATA section
EntityReference name of entity referenced null
Entity entity name null
ProcessingInstruction target entire content excluding target
Comment #comment content of the comment
Document #document null
DocumentType document type name null
DocumentFragment #document-fragment null
Notation notation name null

Examples

Code:
var xml_doc = new
ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("states.xml");
var current_node = xml_doc.documentElement.childNodes.item(0)
document.write(current_node.xml);
Output:
Florida Tallahassee
Explanation:

Microsoft's XMLDOMNode object extends the core XML DOM node interface with support for data types, namespaces, DTDs, and schemas. To demonstrate how it might be used we shall use a simple XML document called 'states.xml' which looks like this:

<States>
   <State ref="FL">
      <name>Florida</name>
      <capital>Tallahassee</capital>
   </State>
   <State ref="IA">
      <name>Iowa</name>
      <capital>Des Moines</capital>
   </State>
</States>


We will now load the XML file and create a Node object called 'current_node' out of the first child node of the root (i.e. the first State). Then we will write the xml for that node to the HTML file.

Language(s): JavaScript

Properties

attributes

Syntax: node.attributes

This is a read-only property that returns a NamedNodeMap for nodes that can have attributes.

baseName

Syntax: document.basename

This is a read-only property that returns the base name for a node.

childNodes

Syntax: node.childNodes

This is a read-only property containing a node list of all children for those elements that can have them.

dataType

Syntax: node.dataType

This is a read-only property that specifies the data type for the node.

definition

Syntax: node.definition

This property returns the definition of the node in the DTD or schema.

firstChild

Syntax: node.firstChild

This is a read-only property that returns the first child node of a node. If there is none, it returns null.

lastChild

Syntax: node.lastChild

This is a read-only property that returns the last child node of a node. If there is none, it returns null.

namespaceURI

Syntax: object.namespaceURI

This property is read-only and returns the URI (Universal Resource Indentifier) of the namespace.

nextSibling

Syntax: node.nextSibling

This property returns the next node in the parent's child list, or null if there is none or the node is of a type that cannot be a child node (Attr, Document, DocumentFragment).

nodeName

Syntax: node.nodeName

This property is read-only and contains the name of the node, depending on type.

nodeType

Syntax: node.nodeType

This property is read-only and contains the name of the node, depending on type.

nodeTypedValue

Syntax: node.nodeTypedValue

This property contains the value of this node expressed in its defined data type.

nodeTypeString

Syntax: node.nodeTypeString

This property is read-only and returns the node type in string form.

nodeValue

Syntax: node.nodeValue

This property contains the value of the node, depending on type.

ownerDocument

Syntax: node.ownerDocument

This property returns the Document object to which the node belongs. If the node itself is a document, then it returns null.

parentNode

Syntax: node.parentNode

This is a read-only property that returns the parent node of all nodes except Document, DocumentFragment and Attr, which cannot have parent nodes.

parsed

Syntax: node.parsed

This property returns a boolean value of true if this node and all of its descendants have been parsed and instantiated. Otherwise it returns false.

prefix

Syntax: object.prefix

Property This property is read-only and returns the namespace prefix, or an empty string if none is specified. For example, it would return 'xxx' for the element xxx:yyy.

previousSibling

Syntax: node.previousSibling

This property returns the previous node in the parent's child list, or null if there is none or the node is of a type that cannot be an child node (Attr, Document, DocumentFragment).

specified

Syntax: node.specified

This property returns a boolean value which indicates whether or not this attribute has a value specified in the XML document.

text

Syntax: node.text

This property contains the text content of this node and its subtrees.

xml

Syntax: node.xml

This property contains the XML representation of this node and its descendants.

Methods

appendChild

Syntax: node.appendChild(tagName)

This method appends a new child node to the list of children for this node.

cloneNode

Syntax: node.cloneNode(deep)

This method creates a clone node which is an exact replica of this node.

hasChildNodes

Syntax: node.hasChildNodes

This method is a convenient way to determine whether a node has child nodes, returning true if it has, and false if not.

insertBefore

Syntax: node.insertBefore(newChild, refChild)

This method is used to insert a new child node before an existing one. If no child node exists, the new child node becomes the first.

removeChild

Syntax: node.removeChild(oldChild)

This method removes the specified node from the list of children and returns it.

replaceChild

Syntax: node.replaceChild(newChild, oldChild)

This method is used to replace one of a node's children with another. It returns the old child.

selectNodes

Syntax: node.selectNodes(patternString)

This method creates a NodeList of all matching descendant nodes returned by the specified pattern-matching operation.

selectSingleNode

Syntax: node.selectSingleNode(patternString)

This method returns an object for the first descendant node to match the specified pattern.

transformNode

Syntax: node.transformNode(stylesheet)

This method processes this node and its descendants using the specified XSL stylesheet, and returns the resulting transformation.

transformNodeToObject

Syntax: node.transformNodeToObject(stylesheet, outputObject)

This method processes this node and its descendants using the specified XSL stylesheet, and returns the resulting transformation in the specified object.