How to create a remove node method in XML DOM

In this articles i am going to explain about how to remove an element and current node in XML DOM.
  • 2540

How to create a remove method in XML DOM

  • The removeChild() method is use to removes a specified node.

We remove an element node

The removeChild() method is use to removes a specified node in XML DOM.

  • If we remove a node then all child nodes are also removed in DOM
  • When a node is removed, all its child nodes are also removed.
  • The following code will remove the first <library> element from the XML

Example

<!DOCTYPE html>

<html>

<head>

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

</script>

</head>

<body>

<script type="text/javascript">

    xmlDoc = loadXMLDoc("library.xml");

 

    document.write("Number of nodes: ");

    document.write(xmlDoc.getElementsByTagName('book').length);

    document.write("<br />");

 

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

    xmlDoc.documentElement.removeChild(y);

 

    document.write("Number of nodes after removeChild(): ");

    document.write(xmlDoc.getElementsByTagName('book').length);

 

</script>

</body>

</html>

 

Define the code

 

  • "loadxmldoc.html" get the data.
  • Set 'a' variable to remove the element node.
  • Remove the element node by using the removeChild() method

We remove current node in XML DOM

  • The removeChild() method is use to removes a specified node in XML DOM.
  • When we have navigated to the node, it is possible to remove that node using the parentNode property and the removeChild() method

Example

<!DOCTYPE html>

<html>

<head>

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

</script>

</head>

<body>

<script type="text/javascript">

    xmlDoc = loadXMLDoc("library.xml");

 

    document.write("Number of nodes before removeChild(): ");

    document.write(xmlDoc.getElementsByTagName("book").length);

    document.write("<br />");

 

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

    a.parentNode.removeChild(x);

 

    document.write("Number of nodes after removeChild(): ");

    document.write(xmlDoc.getElementsByTagName("book").length);

 

</script>

</body>

</html>

 

Define the code

 

  • "loadxmldoc.html" get the data.
  • Set 'a' variable to remove the element node.
  • Remove the element node by using the parentNode property and the removeChild() method

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

© 2020 DotNetHeaven. All rights reserved.