XML DOM Delete Node
- Previous Page DOM Change Node
- Next Page DOM Replace Node
The removeChild() method deletes the specified node.
The removeAttribute() method deletes the specified attribute.
Example
The following example uses an XML file books.xml.
Function loadXMLDoc(), located in external JavaScript, used to load XML files.
- Delete element node
- In this example, removeChild() is used to delete the first <book> element.
- Delete the current element node
- In this example, parentNode and removeChild() are used to delete the current <book> element.
- Delete a text node
- In this example, removeChild() is used to delete the text node of the first <title> element.
- Clear the text of the text node
- In this example, the nodeValue() property is used to clear the text node of the first <title> element.
- Delete attribute based on name
- In this example, removeAttribute() is used to delete the "category" attribute from the first <book> element.
- Delete attribute based on object
- In this example, removeAttributeNode() is used to delete all attributes of the <book> element.
Delete element node
The removeChild() method deletes the specified node.
When a node is deleted, all its child nodes will also be deleted.
The following code snippet will remove the first <book> element from the loaded xml:
xmlDoc=loadXMLDoc("books.xml"); y=xmlDoc.getElementsByTagName("book")[0]; xmlDoc.documentElement.removeChild(y);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load into xmlDoc
- Set the variable y to the element node to be deleted
- Delete an element node from the parent node using the removeChild() method
Delete itself - delete the current node
The removeChild() method is the only method that can delete a specified node.
When you have located the node to be deleted, you can delete this node by using the parentNode property and the removeChild() method:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book")[0]; x.parentNode.removeChild(x);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load into xmlDoc
- Set the variable y to the element node to be deleted
- Delete this element node by using the parentNode property and the removeChild() method
Delete a text node
The removeChild() method can be used to delete a text node:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; y=x.childNodes[0]; x.removeChild(y);
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load into xmlDoc
- Set the variable x to the first title element node
- Set the variable y to the text node to be deleted
- Remove a node from the parent node using the removeChild() method
Not very commonly used removeChild() to remove text from a node. You can use the nodeValue property instead. See the next section.
Clear the text node
The nodeValue property can be used to change or clear the value of the text node:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0].childNodes[0]; x.nodeValue="";
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load into xmlDoc
- Set the variable x to the text node of the first title element
- Use the nodeValue property to clear the text of the text node
Loop and change the text nodes of all <title> elements: TIY
Delete an attribute node based on the name
The removeAttribute(name) method is used to delete an attribute node based on the name.
Example: removeAttribute('category')
The following code snippet deletes the "category" attribute from the first <book> element:
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book"); x[0].removeAttribute("category");
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load into xmlDoc
- Use getElementsByTagName() to obtain the book node
- Delete the "category" attribute from the first book element node
Traverse and delete the "category" attribute of all <book> elements: TIY.
Delete attribute nodes based on the object
The removeAttributeNode(node) method deletes the attribute node by using a Node object as a parameter.
Example: removeAttributeNode(x)
The following code snippet deletes all attributes of all <book> 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); } }
Example Explanation:
- By using loadXMLDoc() Put "books.xml" Load into xmlDoc
- Use getElementsByTagName() to get all book nodes
- Check if each book element has an attribute
- If there is an attribute in a book element, delete the attribute
- Previous Page DOM Change Node
- Next Page DOM Replace Node