XML DOM Remove Node
The removeChild()
method removes the specified node.
The removeAttribute()
method removes the specified attribute.
Try It Yourself - Examples
The following examples use the XML file books.xml. The loadXMLDoc() function, located in an external JavaScript, is used to load the XML file.
Remove Element Node
The removeChild()
method removes the specified node.
When a node is removed, all its child nodes are also removed.
The following code snippet will remove the first <book>
element from the loaded XML:
Example
Example Explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Set the variable
y
to the element node to be removedRemove the element node from its parent node using the
removeChild()
method
Remove Itself - Remove the Current Node
The removeChild()
method is the only method that can remove a specified node.
When you have navigated to the node you want to remove, you can remove it by using the parentNode
property and the removeChild()
method:
Example
Example Explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Set the variable
y
to the element node to be removedRemove this element node using the
parentNode
property and theremoveChild()
method
Remove Text Node
The removeChild()
method can be used to remove text nodes:
Example
Example Explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Set the variable
x
to the first title element nodeSet the variable
y
to the text node to be removedRemove the element node from its parent node using the
removeChild()
method
It is less common to use removeChild()
to remove text from a node. You can use the nodeValue
property 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
Example Explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Set the variable
x
to the text node of the first title elementUse the
nodeValue
property to clear the text of the text node
Traverse and change the text nodes of all <title>
elements: Try It Yourself
Remove Attribute Node by Name
The removeAttribute(name)
method is used to remove an attribute node by name.
Example: removeAttribute('category')
The following code snippet removes the "category" attribute from the first <book>
element:
Example
Example Explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Use
getElementsByTagName()
to get the book nodesRemove the "category" attribute from the first book element node
Traverse and remove the "category" attribute from all <book>
elements: Try It Yourself
Remove Attribute Node by Object
The removeAttributeNode(node)
method removes an attribute node using the node object as a parameter.
Example: removeAttributeNode(x)
The following code snippet removes all attributes from 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);
}
}
Example Explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Use
getElementsByTagName()
to get all book nodesCheck if each book element has attributes
If attributes exist in a book element, remove them ```