XML DOM Remove Nodes

To remove a specified node, the removeChild() method is used, while a specified attribute can be removed using the removeAttribute() method.

Remove an Element Node:

A specified node is removed by the removeChild() method. Removing a node means removing all its child nodes along with it.

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    Author 3
    Author 4
    2005
    350.00
  


Example:





Output: Explanation:

In the above example, we are removing the first <book> element from the books.xml. For this, we will first load “books.xml” in the xmlDoc. The element node to be removed will be set as variable y. Using the removeChild() method, we will remove the element node by from the parent node.

Remove the Current Node:

To remove a specified node, there is only one way and that is the removeChild() method. Using the parentNode property and the removeChild() method, we can remove the desired node after navigating to it.

Example:





Output:

Explanation:

We will first load “books.xml” in the xmlDoc. The element node to be removed will be set as the variable y. The parentNode property and the removeChild() method can then be used to remove the element node.

Remove a Text Node:

We can also remove a text node using the removeChild() method.

Example:





Output:

Explanation:

In the above example, we are loading “books.xml” in the xmlDoc. The first title element node will be set as the variable x and the text node to be removed will be set as the variable y. Using the removeChild() method, we will remove the element node from the parent node.

Clear a Text Node:

The nodeValue property is commonly used to remove the text from a node instead of the removeChild() method. We can change the value of a text node using the nodeValue property.

Example:





Output:

Explanation:

In the above example, we are loading “books.xml” in the xmlDoc to get the first title element’s first child node. We will then clear the text from the text node using the nodeValue property.

Remove an Attribute Node by Name:

To remove an attribute node by its name, the removeAttribute() method can be used.

Example: To remove the “category” attribute in the first <book> element:

removeAttribute('category')

Example:





Explanation:

In the above example, we are loading “books.xml” in the xmlDoc. We will then get the book nodes using the getElementsByTagName() method. Now, from the first book element node, we will remove the “category” attribute.

Example: To loop through and to remove the “category” attribute of all <book> elements:





Output:

Remove Attribute Nodes by Object:

To remove an attribute node, using the node object as a parameter, the removeAttributeNode() method is used.

Example: To remove all the attributes of all the <book> elements:

removeAttributeNode(x)

Example:





Explanation:

In the above example, we are loading “books.xml” in the xmlDoc. We will then get the book nodes using the getElementsByTagName() method. We will then check if there are any attributes for each book element. We will remove the attribute if there is an attribute in a “book” element.

Please follow and like us:
Content Protection by DMCA.com