Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM - Create a node


May 27, 2021 XML DOM


Table of contents


XML DOM creates nodes

XML DOM has a method for creating all node types by providing the method with a name (when needed) and the content or other parameters of those nodes with content, such as text nodes, so that nodes can be created.

XML DOM - Create a node

Try it - instance

The following example uses the XML file .xml.
The function loadXMLDoc(), located in an external JavaScript, is used to load XML files.

Create an element node
This example uses createElement() to create a new element node and adds it to a node using appendChild().

Create property nodes using createAttribute
This example uses createAttribute() to create a new property node and inserts it into an element using setAttributeNode().

Create a property node with setAttribute
This example uses setAttribute() to create a new property for an element.

Create a text node
This example uses createTextNode() to create a new text node and add it to an element using appendChild().

Create a CDATA section node
This example uses createCDATAsection() to create a CDATA section node and add it to an element using appendChild().

Create a comment node
This example uses createComment() to create a comment node and adds it to an element using appendChild().


Create a new element node

CreateElement() method to create a new element node:

xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Create a new element node.
  3. Append the element node to the first element

Traverse and add an element to all the elements: Try


Create a new property node

CreateAttribute() is used to create a new property node:

xmlDoc=loadXMLDoc("books.xml");

newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";

x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Create a new property node "edition"
  3. Set the value of the property node to "first"
  4. Add this new property node to the first element

Traverse all the elements and add a new property node: Try it

Note: If the property already exists, it is replaced by the new property.


Create properties using setAttribute().

Since the setAttribute() method can create a new property without the property being present, we can use this method to create a new property.

xmlDoc=loadXMLDoc("books.xml");

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

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Set (create) the "edition" property with a value of "first" for the first element

Traverse all the elements and add a new property: Try


Create a text node

CreateTextNode() method to create 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);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Create a new element node .
  3. Create a new text node with text that is "first"
  4. Append a new text node to this element node
  5. Append a new element node to the first element

Add an element node with a text node to all the elements: Try


Create a CDATA Section node

CreateCDATASection() method to create a new CDATA section node.

xmlDoc=loadXMLDoc("books.xml");

newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Create a new CDATA section node
  3. Append this new CDATA section node to the first

Traverse and add a CDATA section to all the elements: Try


Create a comment node

CreateComment() method to create a new comment node.

xmlDoc=loadXMLDoc("books.xml");

newComment=xmlDoc.createComment("Revised March 2008");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Create a new comment node
  3. Add this new comment node to the first element

Loop and add a comment node to all the elements: Try

When you create new nodes, there are several ways to insert them into the tree, and we'll describe how to insert them in the next section.