Método replaceData() do XML DOM
Definição e uso
replaceData()
Método substitui os dados do nó CDATA.
Sintaxe
CDATANode.replaceData(start,length,string)
Parâmetros | Descrição |
---|---|
start | Obrigatório. Especifica a posição a partir da qual os caracteres devem ser substituídos. O valor de início é zero. |
length | Obrigatório. Especifica o número de caracteres a serem substituídos. |
string | Obrigatório. Especifica a string a ser inserida. |
Exemplo
O código a seguir carrega "books_cdata.xml" para xmlDoc e substitui "Stunning" por "Fantastic" no nó CDATA do primeiro elemento <html>:
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, "Fantástico"); document.getElementById("demo").innerHTML = x.data; }