XML DOM Create Node
- Previous Page DOM Replace Node
- Next Page DOM Add Node
Instance
The following examples use XML files books.xml.
Function loadXMLDoc(), located in external JavaScript, used to load XML files.
- Create an element node
- This example uses createElement() to create a new element node and then appends it to a node using appendChild().
- Create an attribute node by using createAttribute
- This example uses createAttribute() to create a new attribute node and then inserts the node into an element using setAttributeNode().
- Create an attribute node by using 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 then appends the text node to an element using appendChild().
- Create a CDATA section node
- This example uses createCDATAsection() to create a CDATA section node and then appends it to an element using appendChild().
- Create Comment Node
- This example uses createComment() to create a comment node and then appends it to an element using appendChild().
Create a new element node
The createElement() method creates a new element node:
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Create a new element node <edition>
- Append this element node to the first <book> element
Traverse and add an element to all <book> elements:TIY
Create a new attribute node
createAttribute() is used to create a new attribute node:
xmlDoc=loadXMLDoc("books.xml"); newatt=xmlDoc.createAttribute("edition"); newatt.nodeValue="first"; x=xmlDoc.getElementsByTagName("title"); x[0].setAttributeNode(newatt);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Create a new attribute node "edition"
- Add this new attribute node to the first <title> element
Traverse all <title> elements and add a new attribute node:TIY
Note: If the attribute already exists, it will be replaced by the new attribute.
Create attributes using setAttribute()
Since setAttribute() can create a new attribute when the attribute does not exist, we can use this method to create new attributes.
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('book'); x[0].setAttribute("edition","first");
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Set (create) an attribute with the value "first" for the first <book> element
Traverse all <title> elements and add a new attribute:TIY
Create a text node
The createTextNode() method creates a new text node:
xmlDoc=loadXMLDoc("books.xml"); newel=xmlDoc.createElement("edition"); newtext=xmlDoc.createTextNode("first"); newel.appendChild(newtext); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newel);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Create a new element node <edition>
- Create a new text node whose text is "first"
- Append a new text node to this element node
- Append a new element node to the first <book> element
Add an element node with a text node to all <book> elements:TIY
Create a CDATA Section node
The createCDATASection() method creates a new CDATA section node.
xmlDoc=loadXMLDoc("books.xml"); newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newCDATA);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Create a new CDATA section node
- Append this new CDATA section node to the first <book> element
Traverse and add a CDATA section to all <book> elements:TIY
Create Comment Node
The createComment() method creates a new comment node.
xmlDoc=loadXMLDoc("books.xml"); newComment=xmlDoc.createComment("Revised March 2008"); x=xmlDoc.getElementsByTagName("book")[0]; x.appendChild(newComment);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load xmlDoc
- Create a new comment node
- Append this new comment node to the first <book> element
Loop through and add a comment node to all <book> elements:TIY
- Previous Page DOM Replace Node
- Next Page DOM Add Node