Método createElementNS() do XML DOM
Definição e uso
createElementNS()
Método cria um nó de elemento com espaço de nomes.
Este método retorna um objeto Element.
Sintaxe
createElementNS(ns,name)
Parâmetros | Descrição |
---|---|
ns | String, especifica o nome do espaço de nomes do nó do elemento. |
name | String, especifica o nome do nó do elemento. |
Exemplo
O código a seguir carrega "books.xml" para xmlDoc e adiciona um nó de elemento com espaço de nomes a cada elemento <book>:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books.xml", true); xhttp.send(); function myFunction(xml) { var x, y, z, i, newel, newtext, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book"); // Usar o espaço de nomes e o nó de texto para criar um nó de elemento for (i = 0; i < x.length; i++) { newel = xmlDoc.createElementNS("p", "edition"); newtext = xmlDoc.createTextNode("Primeiro"); newel.appendChild(newtext); x[i].appendChild(newel); } // Imprimir todos os title e edition y = xmlDoc.getElementsByTagName("title"); z = xmlDoc.getElementsByTagNameNS("p","edition"); for (i = 0; i < y.length; i++) { txt += y[i].childNodes[0].nodeValue + " - " + z[i].childNodes[0].nodeValue + " edição." + "Namespace: " + z[i].namespaceURI + "<br>"; } document.getElementById("demo").innerHTML = txt; }