XML DOM - create nodes
Try - Example
The following example uses XML files the Books.xml .
Function loadXMLDoc () , in an external JavaScript is 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 using createAttribute
This example uses createAttribute () to create a new attribute node, and setAttributeNode () to insert it into an element.
Create an attribute node using setAttribute
This example uses setAttribute () to create a new attribute to 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 new element node
createElement () method creates a new element node:
Examples
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Creates a new element node <edition>
- Add this element node to the first <book> element
Traversal to all <book> elements add an element: try
Create a new attribute node
createAttribute () is used to create a new attribute node:
Examples
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Create a new attribute node "edition"
- Setting an attribute node is "first"
- Add this new attribute node to the first <title> element
Through all <title> elements and add a new attribute node: a try
Note: If the property exists, were new property instead.
Use setAttribute () to create properties
Since the setAttribute () method to create a new property while the property does not exist, we can use this method to create a new property.
Examples
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- For the first <book> element is set to (create) the value "first" of the "edition" attribute
Through all <title> elements and add a new property: try
Create a text node
createTextNode () method creates a new text node:
Examples
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Create a new element node <edition>
- Create a new text node whose text is "first"
- Append new text node to the element node
- Add a new element node to the first <book> element
Adding an element node with a text node to all <book> elements: Try
Create CDATA Section Node
createCDATASection () method creates a new CDATA section node.
Examples
newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Create a new CDATA section node
- Add this new CDATA section node to the first <book> element
Traversal to all <book> element to add a CDATA section: try
Create a comment node
createComment () method to create a new comment node.
Examples
newComment=xmlDoc.createComment("Revised March 2008");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Create a new comment node
- Append the new annotation node to the first <book> element
Circulating to all <book> element to add a comment nodes: Try