This page contains the XML for <SCRIPT> DOMNodeList object documentation.
Return to W3C DOM Parser Documentation Main Page
-
CONTENTS
-
DOMNodeList - object description
-
-
-
Native Methods:
-
DOMNodeList - getLength method
-
DOMNodeList - item method
-
-
Inherited Methods:
-
The DOMNodeList object has no inherited W3C methods
DOMNodeList - object description
The DOMNodeList interface provides the abstraction of an ordered collection of
nodes. DOMNodeLists are returned when DOM methods such as getElementsByTagName and
getChildNodes are invoked.
DOMNodeList objects in the DOM are live - meaning changes to the DOMNodeList are visible
in the DOMDocument as soon as they happen.
The items in the NodeList are accessible via an integral index, starting from 0.
DOMNodeList - getLength method
W3C DOM Level 1
domNodeList.getLength();
- or -
domNodeList.length;
accepts:
N/A
returns:
int - the number of DOMNodes included in the DOMNodeList
throws:
N/A
The getLength method of the DOMNodeList returns the number of nodes included in the
DOMNodeList.
NOTE: If a DOM query (such as getElementsByTagName) is
called that finds no applicable nodes, the returned object is a complete DOMNodeList,
*not* a null object. This DOMNodeList will include no DOMNodes and will return 0
when its getLength method is called.
NOTE: It is also possible to access the length of the
DOMNodeList by accessing the length property. This property should be treated as
read-only and should never be set in code.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE>"
+ "<TAG1>"
+ "Hello World"
+ "<!--comment-->"
+ "<![CDATA[this child is of <<<>nodeType CDATA]]>"
+ "<?hello world?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the "TAG1" element
var tag1 = docRoot.getElementsByTagName("TAG1").item(0);
//call getChildNodes to get a DOMNodeList object
//NOTE: getChildNodes does *not* return an array.
//It returns a DOMNodeList object
var domNodeList = tag1.getChildNodes();
//call the getLength method of the DOMNodeList object
//the following should be 4
alert(domNodeList.getLength());
}// end function xmljsDOMExample
DOMNodeList - item method
W3C DOM Level 1
domNodeList.item(<index>);
accepts:
int index - The zero-based index of the node to return
returns:
DOMNode - the DOMNode at <index> or null if <index> out of bounds
throws:
N/A
In order to access a specific node in a DOMNodeList, the DOMNodeList object provides
the index method. The index method allows for a specific DOMNode to be returned from the
DOMNodeList via a zero-based index.
Example:
function xmljsDOMExample() {
var xml;
xml = ""
+ "<?xml version=\"1.0\"?>"
+ "<ROOTNODE>"
+ "<TAG1>"
+ "Hello World"
+ "<!--this is a comment node-->"
+ "<![CDATA[this is a CDATA node]]>"
+ "<?hello this is a processing instruction node?>"
+ "</TAG1>"
+ "</ROOTNODE>";
//instantiate the W3C DOM Parser
var parser = new DOMImplementation();
//load the XML into the parser and get the DOMDocument
var domDoc = parser.loadXML(xml);
//get the root node
var docRoot = domDoc.getDocumentElement();
//get the TAG1 element
var tag1 = docRoot.getFirstChild();
//call getChildNodes on tag1 to get a DOMNodeList object
//NOTE: getChildNodes does *not* return an array.
//It returns a DOMNodeList object
var domNodeList = tag1.getChildNodes();
//call the getLength method of the DOMNodeList object
//the following should be 4
alert(domNodeList.getLength());
//get each DOMNode in the DOMNodeList
//and display its value. Remember, the item method
//of the DOMNodeList is zero-based.
var node;
//get the "Hello World" node from the DOMNodeList
node = domNodeList.item(0);
//the following should be "Hello World"
alert(node.getNodeValue());
//get the comment node from the DOMNodeList
node = domNodeList.item(1);
//the following should be "this is a comment node"
alert(node.getNodeValue());
//get the CDATA node from the DOMNodeList
node = domNodeList.item(2);
//the following should be "this is a CDATA node"
alert(node.getNodeValue());
//get the processing instruction node from the DOMNodeList
node = domNodeList.item(3);
//the following should be "this is a processing instruction node"
alert(node.getNodeValue());
}// end function xmljsDOMExample