XML DOM ノードの作成
- 前のページ DOM ノードの置換
- 次のページ DOM ノードの追加
新しい要素ノードを作成
createElement()
新しい要素ノードを作成する方法:
例 1
newElement = xmlDoc.createElement("edition"); xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
例の説明:
- books.xmlがロードされていると仮定します
xmlDoc
中 - 新しい要素ノード <edition> を作成
- この要素ノードを最初の <book> 要素に追加します:
例 2
すべての <book> 要素をループして要素を追加:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第1版"); newEle.appendChild(newText); x[i].appendChild(newEle); }
新しい属性ノードを作成
createAttribute()
新しい属性ノードを作成するために使用:
例 1
newAtt = xmlDoc.createAttribute("edition"); newAtt.nodeValue = "第一版"; xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
例の説明:
- books.xmlが以下にロードされています:
xmlDoc
中 - 新しい属性ノードを作成
"edition"
- この属性ノードの値を設定します:
"first"
- この新しい属性ノードを最初の <title> 要素に追加します:
例 2
すべての <title> 要素をループして新しい属性ノードを追加:
for (i = 0; i < xLen; i++) { newAtt = xmlDoc.createAttribute("edition"); newAtt.value = "第一版"; x[i].setAttributeNode(newAtt); }
既に存在する場合、新しい属性に置き換えます。
setAttribute()を使用して属性を作成
以下の理由で: setAttribute()
属性が存在しない場合に新しい属性を作成するため、新しい属性を作成するためにも使用できます。
例 1
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
例の説明:
- books.xmlがロードされていると仮定します
xmlDoc
中 - 最初の <book> 要素の
"edition"
属性の値を設定します:"first"
例 2
すべての <title> 要素をループして新しい属性を追加:
for(i = 0; i < x.length; i++) { x[i].setAttribute("edition", "第一版"); }
テキストノードを作成
createTextNode()
新しいテキストノードを作成する方法:
例 1
newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("first"); newEle.appendChild(newText); xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
例の説明:
- books.xmlがロードされていると仮定します
xmlDoc
中 - 新しい要素ノード <edition> を作成します
- テキストを含む新しいテキストノードを作成します
"first"
- 新しいテキストノードを新しい要素ノードに追加します
- 新しい要素ノードを最初の <book> 要素に追加します
例 2
テキストノードを持つ要素ノードをすべての <book> 要素に追加します:
for (i = 0; i < xLen; i++) { newEle = xmlDoc.createElement("edition"); newText = xmlDoc.createTextNode("第1版"); newEle.appendChild(newText); x[i].appendChild(newEle); }
CDATA Section ノードを作成します
createCDATASection()
新しい CDATA section ノードを作成するメソッドです。
例 1
newCDATA = xmlDoc.createCDATASection("新年特別割引 & 限定オファー"); xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
例の説明:
- books.xmlがロードされていると仮定します
xmlDoc
中 - 新しい CDATA section ノードを作成します
- 新しい CDATA ノードを最初の <book> 要素に追加します
例 2
ループしてすべての <book> 要素に CDATA 部分を追加します:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length; newtext = "新年特別割引 & 限定オファー"; for (i = 0; i < xLen; i++) { newCDATA = xmlDoc.createCDATASection(newtext); x[i].appendChild(newCDATA); }
コメントノードを作成します
createComment()
新しいコメントノードを作成するメソッドです。
例 1
newComment = xmlDoc.createComment("2024 年 2 月改訂"); xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);
例の説明:
- books.xmlがロードされていると仮定します
xmlDoc
中 - 新しいコメントノードを作成します
- 新しいコメントノードを最初の <book> 要素に追加します
例 2
ループしてすべての <book> 要素にコメントノードを追加します:
x = xmlDoc.getElementsByTagName("book"); xLen = x.length for (i = 0; i < xLen; i++) { newComment = xmlDoc.createComment("2024 年 2 月改訂"); x[i].appendChild(newComment); }
- 前のページ DOM ノードの置換
- 次のページ DOM ノードの追加