getParentNode (DOMNode - JavaScript)

Gets the parent node.

Defined in

DOMNode

Syntax

getParentNode() : DOMNode

Return value Description
DOMNode The parent node or null. This should be null only if called from the root.

Examples

This button uses a recursive function to get all the nodes of a DOM. The script displays the name and if applicable the value of each node using indentation to indicate the level.
// display newline, tabs, node
function displaynode(node, nt) {
	requestScope.y = requestScope.y + nt + 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 root
	var dom = doc.getDOM();
	requestScope.y = dom.getNodeName();
	// get first node below root 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:
#document
schema0
	book1
		element0
			#text = foo
		element1
			#text = bar
	book2
		element0
			#text = foo
		element1
			#text = bar