XML DOM replaceData() メソッド
定義と使用法
replaceData()
方法は、CDATAセクションのデータを置き換えます。
文法
CDATANode.replaceData(start,length,string)
パラメータ | 説明 |
---|---|
start | 必須。置き換えを開始する場所を指定します。開始値は0から始まります。 |
length | 必須。置き換える文字数を指定します。 |
string | 必須。挿入する文字列を指定します。 |
例
以下のコードは、"books_cdata.xml"をxmlDocに読み込み、最初の<html>要素のCDATAセクションで"Stunning"を"Fantastic"に置き換えます:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books_cdata.xml", true); xhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("html")[0].childNodes[0]; x.replaceData(3, 8, "Fantastic"); document.getElementById("demo").innerHTML = x.data; }