XML DOM Delete Node


The removeChild() method deletes the specified node.

The removeAttribute() method deletes the specified attribute.


实例

Try it yourself - Example

The example below uses an XML filebooks.xml。
FunctionloadXMLDoc(), located in external JavaScript, used to load the XML file.

Remove Element Node
This example uses removeChild() to delete the first <book> element.

Delete the current element node
This example uses parentNode and removeChild() to delete the current <book> element.

Remove Text Node
This example uses removeChild() to delete the text node of the first <title> element.

Clear the text of a text node
This example uses the nodeValue() property to clear the text node of the first <title> element.

Delete attribute by name
This example uses removeAttribute() to delete the "category" attribute from the first <book> element.

Delete attribute by object
This example uses removeAttributeNode() to delete all attributes from all <book> elements.


Delete element nodes

The removeChild() method deletes the specified node.

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

The following code snippet will delete the first <book> element from the loaded xml:

Example

xmlDoc=loadXMLDoc("books.xml");

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

xmlDoc.documentElement.removeChild(y);

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Set variable y to the element node to be deleted.
  3. Delete the element node from the parent node by using the removeChild() method.

Delete self - Delete the current node

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

When you have navigated to the node you need to delete, you can delete this node by using the parentNode property and the removeChild() method:

Example

xmlDoc=loadXMLDoc("books.xml");

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

x.parentNode.removeChild(x);

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Set variable y to the element node to be deleted.
  3. Delete this element node by using the parentNode property and the removeChild() method.

Delete text nodes

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

Example

xmlDoc=loadXMLDoc("books.xml");

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

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

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Set variable x to the first title element node.
  3. Set variable y to the text node to be deleted.
  4. Delete the element node from the parent node by using the removeChild() method.

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


Clear text nodes

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

Example

xmlDoc=loadXMLDoc("books.xml");

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

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Set variable x to the text node of the first title element.
  3. Use the nodeValue property to clear the text of the text node.

Loop through and change the text nodes of all <title> elements:Try it Yourself


Delete attribute node by name

removeAttribute(name) method is used to delete an attribute node by name.

Example: removeAttribute('category')

The following code snippet deletes the "category" attribute from the first <book> element:

Example

xmlDoc=loadXMLDoc("books.xml");

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

Try it Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Use getElementsByTagName() to get book nodes
  3. Delete the "category" attribute from the first book element node.

Loop through and delete the "category" attribute of all <book> elements:Try it Yourself


Delete attribute node by object

removeAttributeNode(node) method deletes an attribute node by using a node object as a parameter.

Example: removeAttributeNode(x)

The following code snippet deletes all attributes of all <book> elements:

Example

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 Yourself »

Example explanation:

  1. UsageloadXMLDoc()put "books.xml" Load into xmlDoc
  2. Use getElementsByTagName() to get all book nodes.
  3. Check whether each book element has attributes.
  4. If an attribute exists in a book element, delete that attribute.

other extensions