XML DOM createElementNS() Method
Definition and Usage
The createElementNS() method can create an element node with a specified namespace.
This method can return an Element object.
Syntax:
createElementNS(ns,name)
Parameter | Description |
---|---|
ns | A string value that can specify the name of the namespace for this element node. |
name | A string value that can specify the name of this element node. |
Descrição
O método createElementNS() é semelhante a Método createElement()Similar, mas ele cria nós Element que além de ter o nome especificado, também têm o namespace especificado. Este método é usado apenas em documentos XML com namespace.
Exemplo
Em todos os exemplos, usaremos o arquivo XML books.xml, e as funções JavaScript loadXMLDoc().
O seguinte trecho de código pode adicionar um nó de elemento com namespace especificado a cada <book>:
mlDoc=loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName('book');
var newel,newtext;
for (i=0;i<x.length;i++)
{
newel=xmlDoc.createElementNS('p','edition')
;
newtext=xmlDoc.createTextNode('First');
newel.appendChild(newtext);
x[i].appendChild(newel);
}