Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM – Creating Nodes

XML DOM Creating Nodes


Try it - Example

The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, 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 with createAttribute
This example uses createAttribute() to create a new attribute node and setAttributeNode() to insert it into an element.

Create an Attribute Node with 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 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 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")[0];

x.appendChild(newel);

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Create a new element node
  3. Add a class name to the first element append this element node

Loop through and append to all element add an element: Try it


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[0].setAttributeNode(newatt);

Try it »

Example explanation:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Create a new attribute node "edition"
  3. Set the attribute node's value to "first"
  4. Add a class name to the first add this new attribute node to the element</li> </ol> <p>Traverse all the <title> element, and add a new attribute node: <a href="/try/try.php?filename=try_dom_createattribute2">Try it</a></p> <p>**Note:** If the attribute already exists, it is replaced by the new attribute.</p> <hr /> <h2>Use setAttribute() to create an attribute</h2> <p>Since the setAttribute() method can create a new attribute if the attribute does not exist, we can use this method to create a new attribute.</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName('book');</p> <p>x[0].setAttribute("edition","first");</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>for the first <book> set (create) the "edition" attribute of the element with the value "first"</li> </ol> <p>Traverse all the <title> element and add a new attribute: <a href="/try/try.php?filename=try_dom_createattribute4">Try it</a></p> <hr /> <h2>Create a text node</h2> <p>The createTextNode() method creates a new text node:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>newel=xmlDoc.createElement("edition");</p> <p>newtext=xmlDoc.createTextNode("first");</p> <p>newel.appendChild(newtext);</p> <p>x=xmlDoc.getElementsByTagName("book")[0];</p> <p>x.appendChild(newel);</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Create a new element node <edition></li> <li>Create a new text node whose text is "first"</li> <li>Append a new text node to this element node</li> <li>to the first <book> append a new element node to an element</li> </ol> <p>to all <book> add an element node with a text node to an element: <a href="/try/try.php?filename=try_dom_createelement">Try it</a></p> <hr /> <h2>Create a CDATA Section node</h2> <p>The createCDATASection() method creates a new CDATA section node.</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>newCDATA=xmlDoc.createCDATASection("Special Offer & Book Sale");</p> <p>x=xmlDoc.getElementsByTagName("book")[0];</p>

    x.appendChild(newCDATA);

    Try it »

    Example explanation:

    1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
    2. Create a new CDATA section node
    3. Add a class name to the first element append this new CDATA section node

    Loop through and append to all element add a CDATA section: Try it


    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")[0];

    x.appendChild(newComment);

    Try it »

    Example explanation:

    1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
    2. Create a new comment node
    3. Append this new comment node to the first Element

    Loop and append to all element add a comment node: Try it