getNodeValue (DOMNode - JavaScript)

ノードの値を取得します。

定義場所

DOMNode

構文

getNodeValue() : string

戻り値 説明
string ノードの値または NULL。

使用法

以下のタイプのノードは値を持ちます。他のタイプのノードは NULL を返します。
クラス ノード値
DOMAttr 属性の値
DOMCDATASection CDATA セクションのコンテンツ
DOMComment コメントのコンテンツ
DOMProcessingInstruction 命令のデータ内容
DOMText テキストのコンテンツ

このボタンは、DOM のすべてのノードを取得するために、取得再帰的関数を使用します。このスクリプトは、 各ノードの名前と、該当する場合は値を表示します。 このとき、インデントを使用してレベルを示します。
// 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";
}
DOM に対する入力 XML が以下のとおりである場合、
<schema0>
  <book1>
    <element0>foo</element0>
    <element1>bar</element1>
  </book1>
  <book2>
    <element0>foo</element0>
    <element1>bar</element1>
  </book2>
</schema0>
ディスプレイには以下のとおり表示されます。
#document
schema0
	book1
		element0
			#text = foo
		element1
			#text = bar
	book2
		element0
			#text = foo
		element1
			#text = bar