Latest web development tutorials

XML DOM - create nodes

Examples

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

xmlDoc=loadXMLDoc("books.xml");

newel=xmlDoc.createElement("edition");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newel);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Creates a new element node <edition>
  3. 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

xmlDoc=loadXMLDoc("books.xml");

newatt=xmlDoc.createAttribute("edition");
newatt.nodeValue="first";

x=xmlDoc.getElementsByTagName("title");
x[0].setAttributeNode(newatt);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Create a new attribute node "edition"
  3. Setting an attribute node is "first"
  4. 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

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName('book');
x[0].setAttribute("edition","first");

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. 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

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"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Create a new element node <edition>
  3. Create a new text node whose text is "first"
  4. Append new text node to the element node
  5. 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

xmlDoc=loadXMLDoc("books.xml");

newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newCDATA);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Create a new CDATA section node
  3. 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

xmlDoc=loadXMLDoc("books.xml");

newComment=xmlDoc.createComment("Revised March 2008");

x=xmlDoc.getElementsByTagName("book")[0];
x.appendChild(newComment);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Create a new comment node
  3. Append the new annotation node to the first <book> element

Circulating to all <book> element to add a comment nodes: Try