XML DOM Creating Nodes
Try it - Example
The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, used to load the XML file.
Create an Element Node
This example uses createElement() to create a new element node and appendChild() to add it to a node.
Create an Attribute Node with createAttribute
This example uses createAttribute() to create a new attribute node and setAttributeNode() to insert it into an element.
Create an Attribute Node with 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 appendChild() to add it to an element.
Create a CDATA Section Node
This example uses createCDATAsection() to create a CDATA section node and appendChild() to add it to an element.
Create a Comment Node
This example uses createComment() to create a comment node and appendChild() to add it to an element.
Create a New Element Node
The createElement() method creates a new element node:
Example
xmlDoc=loadXMLDoc("books.xml");
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Create a new element node
- Add a class name to the first
element append this element node
Loop through and append to all
Create a New Attribute Node
createAttribute() is used to create a new attribute node:
Example
xmlDoc=loadXMLDoc("books.xml");
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Create a new attribute node "edition"
- Set the attribute node's value to "first"
- Add a class name to the first
add this new attribute node to the element
Traverse all the
**Note:** If the attribute already exists, it is replaced by the new attribute.
Use setAttribute() to create an attribute
Since the setAttribute() method can create a new attribute if the attribute does not exist, we can use this method to create a new attribute.
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- for the first
set (create) the "edition" attribute of the element with the value "first"
Traverse all the
Create a text node
The createTextNode() method creates a new text node:
Example
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 »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- Create a new element node
- Create a new text node whose text is "first"
- Append a new text node to this element node
- to the first
append a new element node to an element
to all
Create a CDATA Section node
The createCDATASection() method creates a new CDATA section node.
Example
xmlDoc=loadXMLDoc("books.xml");
newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Create a new CDATA section node
- Add a class name to the first
element append this new CDATA section node
Loop through and append to all
Create a Comment Node
The createComment() method creates a new comment node.
Example
xmlDoc=loadXMLDoc("books.xml");
newComment=xmlDoc.createComment("Revised March 2008");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Create a new comment node
- Append this new comment node to the first
Element
Loop and append to all