Or something like:
<?php
while ($node->childNodes->length)
$node->removeChild($node->firstChild);
?>
DOMNode::removeChild
(No version information available, might be only in CVS)
DOMNode::removeChild — Supprime un fils de la liste des enfants
Description
Cette fonction supprime un fils de la liste des enfants.
Liste de paramètres
- oldnode
-
Le fils à effacer.
Valeurs de retour
Si le fils ne peut être effacé, la fonction retourne l'ancien fils.
Erreurs / Exceptions
- DOM_NO_MODIFICATION_ALLOWED_ERR
-
Lancé si le noeud est en lecture seule.
- DOM_NOT_FOUND
-
Lancé si oldnode n'est pas un fils de ce noeud.
Exemples
L'exemple suivant va effacer l'élément chapter de notre document XML.
Exemple #1 Effacement d'un fils
<?php
$doc = new DOMDocument;
$doc->load('book.xml');
$book = $doc->documentElement;
// Nous récupérons le chapitre et l'effaçons du livre
$chapter = $book->getElementsByTagName('chapter')->item(0);
$oldchapter = $book->removeChild($chapter);
echo $doc->saveXML();
?>
L'exemple ci-dessus va afficher :
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN" "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd"> <book id="listing"> <title>My lists</title> </book>
DOMNode::removeChild
tim cameron ryan
19-Jul-2008 01:00
19-Jul-2008 01:00
Michael D (DigitalGemstones.com)
05-Jul-2008 04:15
05-Jul-2008 04:15
Note that iterating through the childNodes array and removing those children will stop the iteration.
For example:
foreach($node->childNodes as $child)
{
if(iWantToDeleteThisNode($child))
$child->parentNode->removeChild($child);
}
Will not work. Note that in removing the node, the childNodes array gets rebuilt (it seems) and so only the first item will be removed. In order to properly remove all the children you want to remove, you will need to do something like the following:
$nodesToDelete = array();
foreach($node->childNodes as $child)
{
if(iWantToDeleteThisNode($child))
$nodesToDelete[] = $child;
}
foreach($nodesToDelete as $node)
{
$node->parentNode->removeChild($node);
}
I believe this is actually more efficient than the first snippet would be (if it worked) but obviously I cannot benchmark it.
Wala
04-Apr-2008 11:57
04-Apr-2008 11:57
I used the following to delete an entire node using the child element:
<?php
$childElement->parentNode->parentNode-> removeChild($childElement->parentNode);
?>
I could not have thought of it without the help of everyone who contributed to this thread.
Thanks!
(I had to introduce the extra space between parentNode->removeChild to get rid of the wordwrap() warning while posting this note)
Vasil Rangelov
06-Oct-2007 10:14
06-Oct-2007 10:14
At the time of writing, I suppose rightfully, removeChild() removes only the selected node, but when you remove an element, it's child elements are not removed. If you want to achieve that, replaceChild() is the solution.
The following should remove all descendants of the $node DOMNode, regardless of it's name:
<?php
$node->replaceChild(new DOMElement($node->nodeName), $node);
?>
If you're replacing the root element, you must explicitly state that with $node->documentElement as the second argument.
lsoethout at hotmail dot com
11-Mar-2007 10:08
11-Mar-2007 10:08
The previous example is not correct; it only works when the tags are direct childs of the top-level node.
Better is:
while($elem = $dom->getElementsByTagName("name")->item(0)) {
$elem->parentNode->removeChild($elem);
}
dave at dgx dot cz
27-Aug-2006 06:10
27-Aug-2006 06:10
remove all children of a given node (simple way):
$node->nodeValue = '';
mytto at openxtrem dot com
07-Sep-2005 02:20
07-Sep-2005 02:20
Back again on removing childs and iterators robustness.
Things get a bit more complicated when you only want to remove 'some' nodes according to a certain condition. Then you can't just remove the first one repeatedly.
The trick is to copy the content of the node list into a more robust collection than DOMNodeList, I name array!
The following piece of code will, for instance, remove all empty child nodes:
<?php
// Copy childNodes array
$childNodes = array();
foreach($node->childNodes as $childNode) {
$childNodes[] = $childNode;
}
// Browse with the copy
foreach ($childNodes as $childNode) {
if (!$childNode->hasChildNodes()); {
$childNode->parentNode->removeChild($childNode);
}
}
?>
karan
28-Aug-2005 02:48
28-Aug-2005 02:48
can we directly remove a node which has some children or do we need to remove the children first.
while($mytn->hasChildNodes())
{
$mytn->removeChild($mytn->childNodes->item(0));
}
echo $j;//this shows correct no of children
echo $mytn->childNodes->length;//after removing it
// shows 0..correct
$dom->save(net.xml);
but the changes r not reflected in the xml file
blaine (at) blainegarrett (dot com)
23-Aug-2005 10:10
23-Aug-2005 10:10
This might be obvious, but might help someone as well...
If you simply have a node that you need to remove (perhaps from an xpath) and don't know the parent node offhand (the examples seem to assume you have the parent in a variable), then you can do something easy like this...
$node->parentNode->removeChild($node);
parentNode is a scalar property of the Element.
Hope that helps.
--
Blaine Garrett
Executive Director of the Art Attack
http://theartattack.org
pedro at leb dot usp dot br
30-May-2005 04:24
30-May-2005 04:24
You may also use the hasChild function:
<?php
while($node->hasChildNodes()) {
$node->removeChild($node->childNodes->item(0));
}
?>
When you remove a childNode, the next node becomes the first one!
vsematika at centrum dot cz
13-Feb-2005 03:35
13-Feb-2005 03:35
For those who don't understand >sbeam at onsetcorps dot net on 02-Feb-2005 12:07< 'hack', here's a little discussion:
First but *wrong* try would be:
<?php
foreach ($parent->childNodes as $child) {
$parent->removeChild($child);
?>
This doesn't work because DOM tree id modified on-the-fly and this confuses foreach loop.
The idea behind sbeam's trick is that after removing the first item in the first iteration, the second item in childNodes nodelist immediately becomes the first item. That's why we must _always_ remove the first child. Here's another implementation:
<?php
$count = $parent->childNodes->length;
for ($i = 0; $i < $count; $i++) {
$oldNode = $parent->removeChild($parent->childNodes->item(0)); // !!! not item($i) !!!
}
?>
sbeam at onsetcorps dot net
02-Feb-2005 09:07
02-Feb-2005 09:07
remove all children of a given node:
while ($parent->childNodes->length) {
$parent->removeChild($parent->childNodes->item(0));
}
