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 | String value, can specify the name of the namespace for this element node. |
name | String value, can specify the name of this element node. |
Description
createElementNS() method is similar to createElement() methodSimilar, but it creates an Element node that has a specified namespace in addition to the specified name. This method is only used in XML documents that use namespaces.
Example
In all examples, we will use the XML file books.xml, and JavaScript functions loadXMLDoc().
The following code snippet can add an element node with a specified namespace to each <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);
}