Create a New Element Node
The createElement() method creates a new element node:
Example
newElement = xmlDoc.createElement("edition");
xmlDoc.getElementsByTagName("book")[0].appendChild(newElement);
Create a New Attribute Node
The createAttribute() is used to create a new attribute node:
Example
newAtt = xmlDoc.createAttribute("edition"); newAtt.nodeValue = "first"; xmlDoc.getElementsByTagName("title")[0].setAttributeNode(newAtt);
If the attribute already exists, it is replaced by the new one.
Create an Attribute Using setAttribute()
Since the setAttribute() method creates a new attribute if the attribute does not exist, it can be used to create a new attribute.
Example
xmlDoc.getElementsByTagName('book')[0].setAttribute("edition","first");
Create a Text Node
The createTextNode() method creates a new text node:
Example
newEle = xmlDoc.createElement("edition");
newText = xmlDoc.createTextNode("first");
newEle.appendChild(newText);
xmlDoc.getElementsByTagName("book")[0].appendChild(newEle);
Create a CDATA Section Node
The createCDATASection() method creates a new CDATA section node.
Example
newCDATA = xmlDoc.createCDATASection("Special Offer & Book Sale");
xmlDoc.getElementsByTagName("book")[0].appendChild(newCDATA);
Create a Comment Node
The createComment() method creates a new comment node.
Example
newComment = xmlDoc.createComment("Revised March 2015");
xmlDoc.getElementsByTagName("book")[0].appendChild(newComment);