XML DOM Deleting Nodes
The removeChild() method deletes the specified node.
The removeAttribute() method deletes the specified attribute.
Try it - Example
The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, used to load the XML file.
Delete an Element Node
This example uses removeChild() to delete the first
Delete the Current Element Node
This example uses parentNode and removeChild() to delete the current
Delete a Text Node
This example uses removeChild() to delete the first
Clear the text of the text node
This example uses the nodeValue() property to clear the first
Remove an attribute by name
This example uses removeAttribute() from the first
Remove an attribute by object
This example uses removeAttributeNode() from all
Delete an element node
The removeChild() method removes the specified node.
When a node is deleted, all its child nodes are also deleted.
The following code snippet removes the first from the loaded xml
Example
xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- Set the variable y to the element node to be deleted
- Delete an element node from its parent node by using the removeChild() method
Delete itself - delete 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 delete, you can delete it by using the parentNode property and the removeChild() method:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded 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 remove a text node:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- Set the variable x to the first title element node
- Set the variable y to the text node to be deleted
- Delete an element node from its parent node by using the removeChild() method
It is less common to use removeChild() to remove text from a node. The nodeValue property can be used instead. See the next section.
Clear the text node
The nodeValue property can be used to change or clear the value of a text node:
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- Set the variable x to the text node of the first title element
- Use the nodeValue property to clear the text of a text node
Traverse and change all
Remove an attribute node by name
removeAttribute(name) method is used to remove an attribute node by name.
Example: removeAttribute('category')
The following code snippet removes the first
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- Use getElementsByTagName() to get the book nodes
- Remove the "category" attribute from the first book element node
Traverse and delete all
Remove an attribute node by object
removeAttributeNode(node) method removes an attribute node by using the node object as a parameter.
Example: removeAttributeNode(x)
The following code snippet deletes all
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
)
;
}
}
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Use getElementsByTagName() to get all book nodes
- Check whether each book element has attributes
- If a book element has attributes, delete the attribute