XML DOM Create Node

Instance

The following examples use XML files books.xml.

Function loadXMLDoc(), located in external JavaScript, used to load XML files.

Create an element node
This example uses createElement() to create a new element node and then appends it to a node using appendChild().
Create an attribute node by using createAttribute
This example uses createAttribute() to create a new attribute node and then inserts the node into an element using setAttributeNode().
Create an attribute node by using setAttribute
This example uses setAttribute() to create a new attribute for an element.
Create a text node
This example uses createTextNode() to create a new text node and then appends the text node to an element using appendChild().
Create a CDATA section node
This example uses createCDATAsection() to create a CDATA section node and then appends it to an element using appendChild().
Create Comment Node
This example uses createComment() to create a comment node and then appends it to an element using appendChild().

Create a new element node

The createElement() method creates a new element node:

xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

Example Explanation:

  1. By using loadXMLDoc() Put "books.xml" Load xmlDoc
  2. Create a new element node <edition>
  3. Append this element node to the first <book> element

TIY

Traverse and add an element to all <book> elements:TIY

Create a new attribute node

createAttribute() is used to create a new attribute node:

xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt); 

Example Explanation:

  1. By using loadXMLDoc() Put "books.xml" Load xmlDoc
  2. Create a new attribute node "edition"
  3. Add this new attribute node to the first <title> element

TIY

Traverse all <title> elements and add a new attribute node:TIY

Note: If the attribute already exists, it will be replaced by the new attribute.

Create attributes using setAttribute()

Since setAttribute() can create a new attribute when the attribute does not exist, we can use this method to create new attributes.

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");

Example Explanation:

  1. By using loadXMLDoc() Put "books.xml" Load xmlDoc
  2. Set (create) an attribute with the value "first" for the first <book> element

TIY

Traverse all <title> elements and add a new attribute:TIY

Create a text node

The createTextNode() method creates a new text node:

xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

Example Explanation:

  1. By using loadXMLDoc() Put "books.xml" Load xmlDoc
  2. Create a new element node <edition>
  3. Create a new text node whose text is "first"
  4. Append a new text node to this element node
  5. Append a new element node to the first <book> element

TIY

Add an element node with a text node to all <book> elements:TIY

Create a CDATA Section node

The createCDATASection() method creates a new CDATA section node.

xmlDoc=loadXMLDoc("books.xml");
newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);

Example Explanation:

  1. By using loadXMLDoc() Put "books.xml" Load xmlDoc
  2. Create a new CDATA section node
  3. Append this new CDATA section node to the first <book> element

TIY

Traverse and add a CDATA section to all <book> elements:TIY

Create Comment Node

The createComment() method creates a new comment node.

xmlDoc=loadXMLDoc("books.xml");
newComment=xmlDoc.createComment("Revised March 2008");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);

Example Explanation:

  1. By using loadXMLDoc() Put "books.xml" Load xmlDoc
  2. Create a new comment node
  3. Append this new comment node to the first <book> element

TIY

Loop through and add a comment node to all <book> elements:TIY