XML DOM - delete nodes
removeChild () method removes a specified node.
removeAttribute () method removes the specified property.
Try - Example
The following example uses XML files the Books.xml .
Function loadXMLDoc () , in an external JavaScript is used to load the XML file.
Remove the element node
This example uses removeChild () to remove the first <book> element.
Remove the current element node
This example uses parentNode and removeChild () to remove the current <book> element.
Delete text node
This example uses removeChild () to remove the first <title> element of the text node.
Clear text node
This example uses the nodeValue () property to clear the first <title> element of the text node.
Remove attribute by name
This example uses removeAttribute () delete "category" attribute from the first <book> element.
Delete attributes according to the object
This example uses removeAttributeNode () to remove all property from all <book> element.
Remove the element node
removeChild () method removes a specified node.
When a node is deleted, all of its child nodes are also removed.
The following code fragment from the loaded xml remove the first <book> element:
Examples
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- The variable y is set to delete the element node
- Remove the element node from the parent node by using the removeChild () method
Remove self - delete the current node
removeChild () method is the only way to remove a specified node.
When you have to navigate to the node to be deleted, you can delete this node by using parentNode property and removeChild () method:
Examples
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- The variable y is set to delete the element node
- By using parentNode property and removeChild () method to remove this element node
Delete text node
removeChild () method can be used to delete text nodes:
Examples
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- The variable x is set to the first title element node
- The variable y is set to text node to be deleted
- Remove the element node from the parent node by using the removeChild () method
Less common removeChild () delete the text from the node. You can use the nodeValue property instead. See the next paragraph.
Empty text nodes
nodeValue property can be used to change the value or empty text node:
Examples
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- The variable x is set to the first title element text node
- Use the nodeValue property to clear the text node
Through and change all the <title> element of the text node: Try
Delete an attribute node by name
removeAttribute(name) method is used to delete an attribute node by name.
Examples: removeAttribute ( 'category')
The following code fragment remove the first <book> element "category" attributes:
Examples
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Use getElementsByTagName () to get the book node
- Delete "category" attribute from the first book element node
Through and remove all <book> element "category" attributes: Try
Remove attribute node based on the object
removeAttributeNode(node) method by using the node object as a parameter to remove an attribute node.
Examples: removeAttributeNode (x)
The following code fragment deletes all the attributes of all <book> element:
Examples
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);
}
}
try it"
Examples explain:
- Use loadXMLDoc () to " the Books.xml " into xmlDoc
- Use getElementsByTagName () to get all the book nodes
- Check that each element has a property book
- If there is property in a book element, remove the property