How to create a node in XML DOM

In this articles I am going to explain about how to create a element node and attribute node.
  • 1806

Create a new element node

  • The createElement() method creates a new element node
  • In this method we append a new element node.

Example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="loadxmldoc.html">

</script>

</head>

<body>

 

<script type="text/javascript">

    xmlDoc = loadXMLDoc("library.xml");

 

    newel = xmlDoc.createElement("combine");

 

    a = xmlDoc.getelementsByTagName("book")[0];

    a.appendChild(newel);

 

    document.write(a.getelementsByTagName("combine")[0].nodeName);

</script>

</body>

</html>

 

Define the code 

  •  "loadxmldoc.html" get the data.
  •  Create a new node element<combine> in this program.
  •  Append the element node to the first <book> element

Create a new attribute node

  • The createattribute() method creates a new attribute node
  • In this method we append a new attribute node.

Example

<!DOCTYPE html>

<html>

<head>

<script type="text/javascript" src="loadxmldoc.html">

</script>

</head>

<body>

 

<script type="text/javascript">

    xmlDoc = loadXMLDoc("library.xml");

 

    newatt = xmlDoc.createAttribute("combine");

    newatt.nodeValue = "first";

 

    a = xmlDoc.getElementsByTagName("title");

    a[0].setAttributeNode(newatt);

 

    document.write("Combine ");

    document.write(x[0].getAttribute("combine"));

 

</script>

</body>

</html>

 

Define the code 

  •   "loadxmldoc.html" get the data.
  •  Create a new node attribute <combine>  in this program.
  •  Append the attribute node to the first <book> element.
  •  Set the value of "First"

You may also want to read these related articles :

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2019 DotNetHeaven. All rights reserved.