XML DOM Delete Nodes

removeChild() Method to delete the specified node.

removeAttribute() Method to delete the specified attribute.

Delete element node

removeChild() Method to delete the specified node.

When a node is deleted, all its child nodes will also be deleted.

This code will delete the first <book> element from the loaded xml:

Example

y = xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);

Try It Yourself

Example Explanation:

  1. Assume books.xml has been loaded into xmlDoc In
  2. Set the variable y Set the element node to be deleted
  3. Use removeChild() Method to remove the element node from the parent node

Delete yourself - delete the current node

removeChild() This is the only method that can delete a specified node.

When you navigate to the node to be deleted, you can use parentNode Attributes and removeChild() Method to delete the node:

Example

x = xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);

Try It Yourself

Example Explanation:

  1. Assume books.xml has been loaded into xmlDoc In
  2. Set the variable y Set the element node to be deleted
  3. Use parentNode Attributes and removeChild() The method to delete element nodes

Delete text nodes

removeChild() The method can also be used to delete text nodes:

Example

x = xmlDoc.getElementsByTagName("title")[0];
y = x.childNodes[0];
x.removeChild(y);

Try It Yourself

Example Explanation:

  1. Assume books.xml has been loaded into xmlDoc In
  2. Set the variable x To the first title element node
  3. Set the variable y Set the text node to be deleted
  4. Use removeChild() The method to remove the node from the parent node

Only use removeChild() It is not common to remove text from a node. You can use the nodeValue attribute instead. See the next section.

Clear the text node

nodeValue The attribute can be used to change or clear the value of a text node:

Example

xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue = "";

Try It Yourself

Example Explanation:

  1. Assume books.xml has been loaded into xmlDoc In
  2. Get the first child node of the first title element
  3. Use nodeValue Attributes can be used to clear the text in a text node

Deleting an attribute node based on the name

removeAttribute() The method is used to delete the attribute node based on the name.

Example 1

The following code removes the 'category' attribute from the first <book> element:

x = xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");

Try It Yourself

Example Explanation:

  1. Assume books.xml has been loaded into xmlDoc In
  2. Use getElementsByTagName() To get the book node
  3. Remove the 'category' attribute from the first book element node to get the book node

Example 2

Loop and delete the 'category' attribute of all <book> elements:

for (i = 0; i < xLen; i++) {
    y = x.item(i);
    y.removeAttribute('category');
}

Try It Yourself

Deleting an attribute node based on an object

removeAttributeNode() The method deletes the attribute node by using a Node object as a parameter.

The following code removes all attributes from all <book> elements:

Example

x = xmlDoc.getElementsByTagName("book");
for (i = 0; i < x.length; i++) {
    while (x[i].attributes.length > 0) {
        attnode = x[i].attributes[0];
        old_att = x[i].removeAttributeNode(attnode);
    }
}

Try It Yourself

Example Explanation:

  1. Assume books.xml has been loaded into xmlDoc In
  2. Use getElementsByTagName() To get all book nodes
  3. Check if each book element has an attribute
  4. If there is an attribute in a book element, then delete the attribute