Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XML DOM – Deleting Nodes

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 the text node of the element.</p> <p><a href="/try/try.php?filename=try_dom_remove_nodevalue">Clear the text of the text node</a><br /> This example uses the nodeValue() property to clear the first <title> the text node of the element.</p> <p><a href="/try/try.php?filename=try_dom_removeattribute">Remove an attribute by name</a><br /> This example uses removeAttribute() from the first <book> remove the "category" attribute from the element.</p> <p><a href="/try/try.php?filename=try_dom_removeattributenode">Remove an attribute by object</a><br /> This example uses removeAttributeNode() from all <book> remove all attributes from the element.</p> <hr /> <h2>Delete an element node</h2> <p>The removeChild() method removes the specified node.</p> <p>When a node is deleted, all its child nodes are also deleted.</p> <p>The following code snippet removes the first from the loaded xml <book> element:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>y=xmlDoc.getElementsByTagName("book")[0];</p> <p>xmlDoc.documentElement.removeChild(y);</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Set the variable y to the element node to be deleted</li> <li>Delete an element node from its parent node by using the removeChild() method</li> </ol> <hr /> <h2>Delete itself - delete the current node</h2> <p>The removeChild() method is the only method that can remove a specified node.</p> <p>When you have navigated to the node you want to delete, you can delete it by using the parentNode property and the removeChild() method:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("book")[0];</p> <p>x.parentNode.removeChild(x);</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Set the variable y to the element node to be deleted</li> <li>Delete this element node by using the parentNode property and the removeChild() method</li> </ol> <hr /> <h2>Delete a text node</h2> <p>The removeChild() method can be used to remove a text node:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("title")[0];</p> <p>y=x.childNodes[0];</p> <p>x.removeChild(y);</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Set the variable x to the first title element node</li> <li>Set the variable y to the text node to be deleted</li> <li>Delete an element node from its parent node by using the removeChild() method</li> </ol> <p>It is less common to use removeChild() to remove text from a node. The nodeValue property can be used instead. See the next section.</p> <hr /> <h2>Clear the text node</h2> <p>The nodeValue property can be used to change or clear the value of a text node:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];</p> <p>x.nodeValue="";</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Set the variable x to the text node of the first title element</li> <li>Use the nodeValue property to clear the text of a text node</li> </ol> <p>Traverse and change all <title> the text node of the element: <a href="/try/try.php?filename=try_dom_remove_nodevalue2">Try it</a></p> <hr /> <h2>Remove an attribute node by name</h2> <p>removeAttribute(<em>name</em>) method is used to remove an attribute node by name.</p> <p>Example: removeAttribute('category')</p> <p>The following code snippet removes the first <book> the "category" attribute in the element:</p> <h2>Example</h2> <p>xmlDoc=loadXMLDoc("books.xml");</p> <p>x=xmlDoc.getElementsByTagName("book");</p> <p>x[0].removeAttribute("category");</p> <p>Try it »</p> <p>Example explained:</p> <ol> <li>Use <a href="dom-loadxmldoc.html">loadXMLDoc()</a> Put the "<a href="/try/demo_source/books.xml">books.xml</a>" loaded into xmlDoc</li> <li>Use getElementsByTagName() to get the book nodes</li> <li>Remove the "category" attribute from the first book element node</li> </ol> <p>Traverse and delete all <book> the "category" attribute of the element: <a href="/try/try.php?filename=try_dom_removeattribute2">Try it</a></p> <hr /> <h2>Remove an attribute node by object</h2> <p>removeAttributeNode(<em>node</em>) method removes an attribute node by using the node object as a parameter.</p> <p>Example: removeAttributeNode(x)</p>

The following code snippet deletes all element attributes:

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:

  1. Use loadXMLDoc() to load "books.xml" load into xmlDoc
  2. Use getElementsByTagName() to get all book nodes
  3. Check whether each book element has attributes
  4. If a book element has attributes, delete the attribute