XML DOM Delete Nodes
- Previous Page DOM Change Node
- Next Page DOM Replace Node
removeChild()
Method to delete the specified node.
removeAttribute()
Method to delete the specified attribute.
Delete element node
removeChild()
Method to delete the specified node.
When a node is deleted, all its child nodes will also be deleted.
This code will delete the first <book> element from the loaded xml:
Example
y = xmlDoc.getElementsByTagName("book")[0]; xmlDoc.documentElement.removeChild(y);
Example Explanation:
- Assume books.xml has been loaded into
xmlDoc
In - Set the variable
y
Set the element node to be deleted - Use
removeChild()
Method to remove the element node from the parent node
Delete yourself - delete the current node
removeChild()
This is the only method that can delete a specified node.
When you navigate to the node to be deleted, you can use parentNode
Attributes and removeChild()
Method to delete the node:
Example
x = xmlDoc.getElementsByTagName("book")[0]; x.parentNode.removeChild(x);
Example Explanation:
- Assume books.xml has been loaded into
xmlDoc
In - Set the variable
y
Set the element node to be deleted - Use
parentNode
Attributes andremoveChild()
The method to delete element nodes
Delete text nodes
removeChild()
The method can also be used to delete text nodes:
Example
x = xmlDoc.getElementsByTagName("title")[0]; y = x.childNodes[0]; x.removeChild(y);
Example Explanation:
- Assume books.xml has been loaded into
xmlDoc
In - Set the variable
x
To the first title element node - Set the variable
y
Set the text node to be deleted - Use
removeChild()
The method to remove the node from the parent node
Only use removeChild()
It is not common to remove text from a node. You can use the nodeValue attribute instead. See the next section.
Clear the text node
nodeValue
The attribute can be used to change or clear the value of a text node:
Example
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue = "";
Example Explanation:
- Assume books.xml has been loaded into
xmlDoc
In - Get the first child node of the first title element
- Use
nodeValue
Attributes can be used to clear the text in a text node
Deleting an attribute node based on the name
removeAttribute()
The method is used to delete the attribute node based on the name.
Example 1
The following code removes the 'category' attribute from the first <book> element:
x = xmlDoc.getElementsByTagName("book"); x[0].removeAttribute("category");
Example Explanation:
- Assume books.xml has been loaded into
xmlDoc
In - Use
getElementsByTagName()
To get the book node - Remove the 'category' attribute from the first book element node to get the book node
Example 2
Loop and delete the 'category' attribute of all <book> elements:
for (i = 0; i < xLen; i++) { y = x.item(i); y.removeAttribute('category'); }
Deleting an attribute node based on an object
removeAttributeNode()
The method deletes the attribute node by using a Node object as a parameter.
The following code removes all attributes from all <book> elements:
Example
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:
- Assume books.xml has been loaded into
xmlDoc
In - Use
getElementsByTagName()
To get all book nodes - Check if each book element has an attribute
- If there is an attribute in a book element, then delete the attribute
- Previous Page DOM Change Node
- Next Page DOM Replace Node