XML DOM β Remove Nodes | Novice Tutorial
removeChild() method removes the specified node.
removeAttribute() method removes the specified attribute.
Try it out - Example
The following examples use the XML file books.xml.
The function loadXMLDoc(), located in external JavaScript, is used to load XML files.
This example uses removeChild() to delete the first element.
This example uses parentNode and removeChild() to delete the current element.
This example uses removeChild() to delete the text node of the first
This example uses the nodeValue() property to clear the text node of the first
This example uses removeAttribute() to delete the "category" attribute from the first element.
This example uses removeAttributeNode() to delete all attributes from all elements.
Delete Element Node
The removeChild() method removes the specified node.
When a node is deleted, all its child nodes are also deleted.
The following code snippet will delete the first element from the loaded xml:
Example
xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book");
xmlDoc.documentElement.removeChild(y);
Example explanation:
- Use loadXMLDoc() to load "[books.xml](" into xmlDoc
- Set variable y to the element node to be deleted
- Delete the element node from the parent node using the removeChild() method
Delete Self - Delete Current Node
The removeChild() method is the only method that can delete a specified node.
When you have navigated to the node that needs to be deleted, you can delete this node by using the parentNode property and the removeChild() method:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x.parentNode.removeChild(x);
Example explanation:
- Use loadXMLDoc() to load "[books.xml](" into xmlDoc
- Set variable y to the element node to be deleted
- Delete this element node using the parentNode property and the removeChild() method
Delete Text Node
The removeChild() method can be used to delete text nodes:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
y=x.childNodes;
x.removeChild(y);
Example explanation:
- Use loadXMLDoc() to load "[books.xml](" into xmlDoc
- Set variable x to the first title element node
- Set variable y to the text node to be deleted
- Delete the element node from the parent node using the removeChild() method
It's not common to use removeChild() to remove text from a node. The nodeValue property can be used instead. See the next section.
Clear Text Node
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").childNodes;
x.nodeValue="";
Example explanation:
- Use loadXMLDoc() to load "[books.xml](" into xmlDoc
- Set variable x to the text node of the first title element
- Use the nodeValue property to clear the text from the text node
Traverse and change the text nodes of all
Delete Attribute Node by Name
The 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 element:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x.removeAttribute("category");
Example explanation:
- Use loadXMLDoc() to load "[books.xml](" into xmlDoc
- Use getElementsByTagName() to get book nodes
- Delete the "category" attribute from the first book element node
Traverse and delete the "category" attribute of all elements: Try it
Delete Attribute Node by Object
The 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 elements:
Example
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book"); for(i=0;i0){attnode=x.attributes; old_att=x.removeAttributeNode(attnode); }}
Example explanation:
- Use loadXMLDoc() to load "[books.xml](" into xmlDoc
- Use getElementsByTagName() to get all book nodes
- Check if each book element has attributes
- If there are attributes in a book element, delete those attributes
YouTip