XML DOM createElementNS() Method

Document object reference manual

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)
Parameters Description
ns The string value can specify the name of the namespace for this element node.
name The 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 for XML documents that use namespaces.

Examples

In all examples, we will use XML files books.xml, as well as JavaScript functions loadXMLDoc().

The following code snippet can add an element node with a specified namespace to each <book>:

xmlDoc=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);
  }

Document object reference manual