getNodeType (DOMNode - JavaScript)

Gets a code representing the node type.

Defined in

DOMNode

Syntax

getNodeType() : short

Return value Description
string The type code of the node.

Usage

The example shows the codes for the node types.

Examples

This button uses a recursive function to get all the nodes of a DOM. The script displays the type, name, and if applicable the value of each node using indentation to indicate the level.
// Change node type to name
function nodetype(node) {
	var name = null;
	switch(node.getNodeType()) {
		case 1 : name = "DOMElement"; break;
		case 2 : name = "DOMAttr"; break;
		case 3 : name = "DOMText"; break;
		case 4 : name = "DOMCDATASection"; break;
		case 5 : name = "DOMEntityReference"; break;
		case 6 : name = "DOMEntity"; break; // ? not sure
		case 7 : name = "DOMProcessingInstruction"; break;
		case 8 : name = "DOMComment"; break;
		case 9 : name = "DOMDocument"; break;
		case 10 : name = "DOMDocumentType"; break;
		case 11 : name = "DOMDocumentFragment"; break;
		case 12 : name = "DOMNotation"; break; // ? not sure
	}
	return name;
}

// display newline, tabs, node
function displaynode(node, nt) {
	requestScope.y = requestScope.y + nt + nodetype(node) + " " + 
		node.getNodeName();
	if(node.getNodeValue() != null)
		requestScope.y = requestScope.y + " = " + node.getNodeValue();
}

// recursive function that gets the next node
function getnext(node, nodep, nt) {
	this.node = node; // node to be displayed
	this.nodep = nodep; // node from previous iteration
	this.nt = nt; // newline and tabs
	// get first child and push down
	// or if no child get next sibling
	if(this.node != null) {
		displaynode(this.node, this.nt);
		if(this.node.getFirstChild() != null) {
			getnext(this.node.getFirstChild(), this.node, this.nt + "\t");
		} else {
			getnext(this.node.getNextSibling(), this.node, this.nt);
		}
	 // or pop up one then get next sibling
	} else {
		if(this.nodep.getParentNode() != null) {
			getnext(this.nodep.getParentNode().getNextSibling(),
			this.nodep.getParentNode(),
			this.nt.left(this.nt.length-1));
		}
	}
}

// main
if (requestScope.n != null
&& requestScope.n < database.getDocumentCount()
&& requestScope.n >= 0) {
	var dc = database.getAllDocuments();
	var doc = dc.getDocumentArray()[requestScope.n];
	// get top node
	var dom = doc.getDOM();
	requestScope.z = dom.getXMLString();
	requestScope.y = nodetype(dom) + " " + dom.getNodeName();
	// get first node below top and start recursive function
	var node = dom.getFirstChild();
	var nt = "\n";
	getnext(node, node, nt);
	
} else {
	requestScope.y = "Error: invalid index";
}
If the input XML to the DOM is as follows:
<schema0>
  <book1>
    <element0>foo</element0>
    <element1>bar</element1>
  </book1>
  <book2>
    <element0>foo</element0>
    <element1>bar</element1>
  </book2>
</schema0>
The display appears as follows:
DOMDocument #document
DOMElement schema0
	DOMElement book1
		DOMElement element0
			DOMText #text = foo
		DOMElement element1
			DOMText #text = bar
	DOMElement book2
		DOMElement element0
			DOMText #text = foo
		DOMElement element1
			DOMText #text = bar