XML DOM appendChild()メソッド
Element オブジェクト
定義と使い方
appendChild() メソッドは、指定した要素ノードの最後の子ノードの後にノードを追加します。
このメソッドは新しい子ノードを返します。
構文
appendChild(node)
| パラメータ | 説明 |
|---|---|
| node | 必須。追加するノード。 |
例 1
次のコードスニペットはloadXMLDoc()を使用して「」を xmlDoc に読み込み、ノード(<edition>)を作成し、最初の <book> 要素の最後の子ノードの後に追加します。
例
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.write(x.getElementsByTagName("edition")[0].nodeName);
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
document.write(x.getElementsByTagName("edition")[0].nodeName);
出力:
edition
試してみる »
例 2
次のコードスニペットはloadXMLDoc()を使用して「」を xmlDoc に読み込み、すべての <book> 要素に新しいノードを追加します。
例
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x[i].appendChild(newel);
}
//すべての title と edition を出力
y=xmlDoc.getElementsByTagName("title");
z=xmlDoc.getElementsByTagName("edition");
for (i=0;i<y.length;i++)
{
document.write(y[i].childNodes[0].nodeValue);
document.write(" - Edition: ");
document.write(z[i].childNodes[0].nodeValue);
document.write("<br>");
}
出力:
Everyday Italian - Edition: First
Harry Potter - Edition: First
XQuery Kick Start - Edition: First
Learning XML - Edition: First
Harry Potter - Edition: First
XQuery Kick Start - Edition: First
Learning XML - Edition: First
試してみる »
Element オブジェクト その他の拡張