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
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Set variable y to the element node to be deleted.
- 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
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Set variable y to the element node to be deleted.
- 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
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load 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 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
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Set variable x to the text node of the first title element.
- 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
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Use getElementsByTagName() to get book nodes
- 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
Try it Yourself »
Example explanation:
- UsageloadXMLDoc()put "books.xml" Load into xmlDoc
- Use getElementsByTagName() to get all book nodes.
- Check whether each book element has attributes.
- If an attribute exists in a book element, delete that attribute.
other extensions