How to create a text node in XML

In this articles I am going to explain about how to create a text node.
  • 2071

Create a attribute node using setattribute method

  • The setattribute() method creates a new attribute node, but the attribute node will be start. then we use the new create attribute node.
  • In this method we append a new attribute node.

Example

<!DOCTYPE html>

<html>

<head>

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

</script>

</head>

<body>

<script type="text/javascript">

    xmlDoc = loadXMLDoc("library.xml");

    a = xmlDoc.getElementsByTagName("title"); 

    a[0].setAttribute("Add", "Some");

    document.write("Add: ");

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

</script>

</body>

</html>

 

Define the code

  • "loadxmldoc.xml" get the data.
  •  Create a new attribute node in this program.
  • Create the attribute "Add" with the value "Some" for the first <book> element

Create a text node in DOM

  • The createTextnode() method creates a new Text node
  • In this method we append a new Text 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("Add");

    newtext = xmlDoc.createTextNode("Some");

    newel.appendChild(newtext);

 

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

    a.appendChild(newel);

    document.write(a.getElementsByTagName("title")[0].childNodes[0] .nodeValue);

    document.write(" - Add: ");

    document.write(a.getElementsByTagName("Add")[0].childNodes[0].nodeValue);

</script>

</body>

</html

Define the code

  • "loadxmldoc.html" get the data.
  •  Create a new Text node in this program.
  • we append a new Text node in the element node
  • We append the element "Add" with the value "Some" for the first <book> element

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.