getNodeValue (DOMNode - JavaScript)
Gets the value of a node.
Defined in
DOMNodeSyntax
getNodeValue() : string
Return value | Description |
---|---|
string |
The value of the node or null. |
Usage
Nodes of the following types have values. Nodes of other types return null.Class | Node value |
---|---|
DOMAttr |
Value of attribute |
DOMCDATASection |
Content of CDATA section |
DOMComment |
Content of comment |
DOMProcessingInstruction |
Data content of instruction |
DOMText |
Content of text |
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