Latest web development tutorials

XML DOM - delete nodes

removeChild () method removes a specified node.

removeAttribute () method removes the specified property.


Examples

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

xmlDoc=loadXMLDoc("books.xml");

y=xmlDoc.getElementsByTagName("book")[0];

xmlDoc.documentElement.removeChild(y);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. The variable y is set to delete the element node
  3. 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

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book")[0];

x.parentNode.removeChild(x);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. The variable y is set to delete the element node
  3. 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

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0];

y=x.childNodes[0];
x.removeChild(y);

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. The variable x is set to the first title element node
  3. The variable y is set to text node to be deleted
  4. 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

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. The variable x is set to the first title element text node
  3. 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

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");

try it"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Use getElementsByTagName () to get the book node
  3. 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

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"

Examples explain:

  1. Use loadXMLDoc () to " the Books.xml " into xmlDoc
  2. Use getElementsByTagName () to get all the book nodes
  3. Check that each element has a property book
  4. If there is property in a book element, remove the property