Create Nodes in XML DOM

Create a new element node

createElement() The method creates a new element node:

Example 1

newElement = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);

Try It Yourself

Example explanation:

  1. Assuming books.xml has been loaded into xmlDoc in
  2. Create a new element node <edition>
  3. Append this element node to the first <book> element

Example 2

Loop through and add an element to all <book> elements:

for (i = 0; i < xLen; i++) { 
    newEle = xmlDoc.createElement("edition");
    newText = xmlDoc.createTextNode("First Edition");
    newEle.appendChild(newText);
    x[i].appendChild(newEle);
{}

Try It Yourself

Create a new attribute node

createAttribute() Used to create a new attribute node:

Example 1

newAtt = xmlDoc.createAttribute("edition");
newAtt.nodeValue = "first edition";
xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);

Try It Yourself

Example explanation:

  1. Assuming that books.xml is loaded into xmlDoc in
  2. Create a new attribute node "edition"
  3. Set the value of this attribute node to "first"
  4. Add this new attribute node to the first <title> element

Example 2

Loop through all <title> elements and add a new attribute node:

for (i = 0; i < xLen; i++) { 
    newAtt = xmlDoc.createAttribute("edition");
    newAtt.value = "first edition";
    x[i].setAttributeNode(newAtt);
{}

Try It Yourself

If the attribute already exists, it will be replaced with the new attribute.

Use setAttribute() to create attributes

Because setAttribute() The method creates a new attribute if it does not exist, so it can also be used to create new attributes.

Example 1

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

Try It Yourself

Example explanation:

  1. Assuming books.xml has been loaded into xmlDoc in
  2. Set the attribute of the first <book> element to "edition" The value of the attribute is set to "first"

Example 2

Loop through all <title> elements and add a new attribute:

for(i = 0; i < x.length; i++) {
    x[i].setAttribute("edition", "first edition");
{}

Try It Yourself

Create a text node

createTextNode() Method to create a new text node:

Example 1

newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("first");
newEle.appendChild(newText);
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);

Try It Yourself

Example explanation:

  1. Assuming books.xml has been loaded into xmlDoc in
  2. Create a new element node <edition>
  3. Create a new text node containing text "first"
  4. Append this new text node to the new element node
  5. Append the new element node to the first <book> element

Example 2

Add elements with text nodes to all <book> elements:

for (i = 0; i < xLen; i++) { 
    newEle = xmlDoc.createElement("edition");
    newText = xmlDoc.createTextNode("First Edition");
    newEle.appendChild(newText);
    x[i].appendChild(newEle);
{}

Try It Yourself

Create CDATA Section node

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

Example 1

newCDATA = xmlDoc.createCDATASection("New Year Sale & Limited Time Discount");
xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);

Try It Yourself

Example explanation:

  1. Assuming books.xml has been loaded into xmlDoc in
  2. Create a new CDATA section node
  3. Append this new CDATA node to the first <book> element

Example 2

Loop through and add CDATA sections to all <book> elements:

x = xmlDoc.getElementsByTagName("book");
xLen = x.length;
newtext = "New Year Sale & Limited Time Discount";
for (i = 0; i < xLen; i++) { 
    newCDATA = xmlDoc.createCDATASection(newtext);
    x[i].appendChild(newCDATA);
{}

Try It Yourself

Create annotation node

createComment() The method creates a new annotation node.

Example 1

newComment = xmlDoc.createComment("2024 February Revision");
xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);

Try It Yourself

Example explanation:

  1. Assuming books.xml has been loaded into xmlDoc in
  2. Create a new annotation node
  3. Append this new annotation node to the first <book> element

Example 2

Loop through and add annotation nodes to all <book> elements:

x = xmlDoc.getElementsByTagName("book");
xLen = x.length
for (i = 0; i < xLen; i++) { 
    newComment = xmlDoc.createComment("2024 February Revision");
    x[i].appendChild(newComment);
{}

Try It Yourself