Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML DOM - Delete nodes


May 27, 2021 XML DOM


Table of contents


XML DOM deletes the node

DOM nodes can be deleted as needed, and this section explains how to delete nodes.

The removeChild() method removes the specified node.

The removeAttribute() method removes the specified property.


XML DOM - Delete nodes

Try it - instance

The following example uses the XML file .xml.
The function loadXMLDoc(), located in an external JavaScript, is used to load XML files.

Remove the element node
In this example, removeChild() is used to remove the first element.

Delete the current element node
In this example, parentNode and removeChild() are used to remove the current element.

Delete the text node
In this example, removeChild() is used to remove the text node of the first element.

Empty the text of the text node
This example uses the nodeValue() property to empty the text node of the first element.

Delete the property by name
This example uses removeAttribute() to remove the "category" property from the first element.

Delete properties based on objects
This example uses removeAttributeNode() to remove all properties from all the elements.


Remove the element node

The removeChild() method removes the specified node.

When a node is deleted, all of its child nodes are also deleted.

The following snippet will remove the first element from the loaded xml:

xmlDoc=loadXMLDoc("books.xml");

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

xmlDoc.documentElement.removeChild(y);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Set the variable y to the element node to be deleted
  3. Remove the element node from the parent node by using the removeChild() method

Delete yourself - Delete the current node

The removeChild() method is the only way to remove the specified node.

When you have navigated to a node that needs to be deleted, you can delete it by using the parentNode property and the removeChild() method:

xmlDoc=loadXMLDoc("books.xml");

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

x.parentNode.removeChild(x);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Set the variable y to the element node to be deleted
  3. This element node is removed by using the parentNode property and the removeChild() method

Delete the text node

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

xmlDoc=loadXMLDoc("books.xml");

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

y=x.childNodes[0];
x.removeChild(y);

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Set variable x to the first title element node
  3. Set the variable y to the text node you want to delete
  4. Remove the element node from the parent node by using the removeChild() method

Less commonly used removeChild() removes text from nodes. Y ou can use the nodeValue property instead of it. Look at the next paragraph.


Empty the text node

The nodeValue property can be used to change or empty the value of a text node:

xmlDoc=loadXMLDoc("books.xml");

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

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Set the variable x to the text node of the first title element
  3. Use the nodeValue property to empty the text of the text node

Traversing and changing the text nodes of all the elements: Try


Delete the property node by name

The removeAttribute (name) method is used to remove property nodes by name.

Example: removeAttribute ('category')

The following snippet removes the "category" property from the first element:

xmlDoc=loadXMLDoc("books.xml");

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

Try it out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Use getElementsByTagName() to get the book node
  3. Remove the "category" property from the first book element node

Traversing and deleting the "category" property of all the elements: Try


Delete the property node based on the object

The removeAttributeNode (node) method removes the property node by using the node object as an argument.

Example: removeAttributeNode (x)

The following snippet removes all the properties of all the elements:

xmlDoc=loadXMLDoc("books.xml");

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 out . . .

Example explanation:

  1. Use loadXMLDoc() to load .xml books" into xmlDoc
  2. Use getElementsByTagName() to get all the book nodes
  3. Check whether each book element has properties
  4. If a property exists in a book element, it is deleted

Related articles

XML DOM removeChild() method