XML DOM Create Node
Try it - Example
The example below uses an XML filebooks.xml。
FunctionloadXMLDoc(), located in external JavaScript, is used to load an XML file.
Create Element Node
This example uses createElement() to create a new element node, and uses appendChild() to add it to a node.
Use createAttribute to Create Attribute Nodes
This example uses createAttribute() to create a new attribute node, and uses setAttributeNode() to insert it into an element.
Using setAttribute to create an attribute node
This example uses setAttribute() to create a new attribute for an element.
Create Text Node
This example uses createTextNode() to create a new text node, and uses appendChild() to add it to an element.
Create CDATA Section Node
This example uses createCDATASection() to create a CDATA section node, and uses appendChild() to append it to an element.
Create Comment Node
This example uses createComment() to create a comment node, and uses appendChild() to add it to an element.
Create a new element node
The createElement() method creates a new element node:
Example
newel=xmlDoc.createElement("edition");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Create a new element node <edition>
- Append this element node to the first <book> element
Loop through and add an element to all <book> elements:Try it Yourself
Create a new attribute node
createAttribute() is used to create a new attribute node:
Example
newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";
x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- 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
Loop through all <title> elements and add a new attribute node:Try it Yourself
Note:If the attribute already exists, it is replaced by the new attribute.
Create an attribute using setAttribute()
Since the setAttribute() method can create a new attribute when the attribute does not exist, we can use this method to create a new attribute.
Example
x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Set (create) an "edition" attribute with the value "first" for the first <book> element
Loop through all <title> elements and add a new attribute:Try it Yourself
Create a text node
The createTextNode() method creates a new text node:
Example
newel=xmlDoc.createElement("edition");
newtext=xmlDoc.createTextNode("first");
newel.appendChild(newtext);
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Create a new element node <edition>
- Create a new text node whose text is "first"
- Append the new text node to this element node
- Append the new element node to the first <book> element
Add an element node with 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
newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Create a new CDATA section node
- Append this new CDATA section node to the first <book> element
Iterate through and add a CDATA section to all <book> elements:Try it Yourself
Create a comment node
The createComment() method creates a new comment node.
Example
newComment=xmlDoc.createComment("Revised March 2008");
x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Create a new comment node
- Append this new comment node to the first <book> element
Loop and add a comment node to all <book> elements:Try it Yourself
other extensions