XML DOM β Create Nodes | Rookie Tutorial
Try it yourself - Examples
The examples below use the XML file books.xml.
The function loadXMLDoc(), located in an external JavaScript, is used to load the XML file.
This example uses createElement() to create a new element node and appends it to a node using appendChild().
Create an attribute node with createAttribute
This example uses createAttribute() to create a new attribute node and inserts it into an element using setAttributeNode().
Create an attribute node with setAttribute
This example uses setAttribute() to create a new attribute for an element.
This example uses createTextNode() to create a new text node and appends it to an element using appendChild().
This example uses createCDATAsection() to create a CDATA section node and appends it to an element using appendChild().
This example uses createComment() to create a comment node and appends it to an element using appendChild().
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");
x.appendChild(newel);
Example explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Create a new element node <edition>
- Append the element node to the first <book> element
Add an element to all <book> elements by looping through them: Try it yourself
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.setAttributeNode(newatt);
Example explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Create a new attribute node "edition"
- Set the value of the attribute node to "first"
- Add this new attribute node to the first <title> element
Add a new attribute node to all <title> elements by looping through them: Try it yourself
Note: If the attribute already exists, it will be replaced by the new one.
Create an Attribute Using setAttribute()
Since the setAttribute() method can create a new attribute if it doesn't exist, we can use this method to create a new attribute.
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book');
x.setAttribute("edition","first");
Example explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Set (create) the "edition" attribute with the value "first" for the first <book> element
Add a new attribute to all <title> elements by looping through them: Try it yourself
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");
x.appendChild(newel);
Example explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Create a new element node <edition>
- Create a new text node with the text "first"
- Append the new text node to the element node
- Append the new element node to the first <book> element
Add an element node containing a text node to all <book> elements: Try it yourself
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");
x.appendChild(newCDATA);
Example explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Create a new CDATA section node
- Append this new CDATA section node to the first <book> element
Add a CDATA section to all <book> elements by looping through them: Try it yourself
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");
x.appendChild(newComment);
Example explanation:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Create a new comment node
- Append this new comment node to the first <book> element
Add a comment node to all <book> elements by looping through them: Try it yourself
YouTip