Create Nodes in XML DOM
- Previous Page DOM Replace Node
- Next Page DOM Add Node
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);
Example explanation:
- Assuming books.xml has been loaded into
xmlDoc
in - Create a new element node <edition>
- 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); {}
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);
Example explanation:
- Assuming that books.xml is loaded into
xmlDoc
in - Create a new attribute node
"edition"
- Set the value of this attribute node to
"first"
- 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); {}
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");
Example explanation:
- Assuming books.xml has been loaded into
xmlDoc
in - 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"); {}
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);
Example explanation:
- Assuming books.xml has been loaded into
xmlDoc
in - Create a new element node <edition>
- Create a new text node containing text
"first"
- Append this new text node to the new element node
- 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); {}
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);
Example explanation:
- Assuming books.xml has been loaded into
xmlDoc
in - Create a new CDATA section node
- 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); {}
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);
Example explanation:
- Assuming books.xml has been loaded into
xmlDoc
in - Create a new annotation node
- 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); {}
- Previous Page DOM Replace Node
- Next Page DOM Add Node