On Tue, Sep 9, 2008 at 12:37 AM, Mario Trojan <mtrojan@xxxxxxxxxxxx> wrote: > Hi Nathan, > > if you're already speaking of iterating children, i'd like to ask you > another question: > > Basically i was trying to do the same thing as Tim, when i experienced some > difficulties iterating over DOMElement->childNodes with foreach and > manipulating strings inside the nodes or even replacing > DOMElement/DOMNode/DOMText with another node. Instead, i am currently > iterating like this: > > $child = $element->firstChild; > while ($child != null) { > $next_sibling = $child->nextSibling; > > // Do something with child (manipulate, replace, ...) > > // Continue iteration > $child = $next_sibling > } > > Is this correct, or is there any better way? i found this the other day on the DOMNodeList page on php.net, essentially foreach will implicitly do what you are doing under the hood, actually, it will also recurse into the children, whereas in this example youve shown, youre only iterating over 1 sub-level of the tree (horizontally across elements at the same level). sometimes it makes sense to drive the iteration yourself as you have shown, but i think the answer to your question is that you must use a reference to the parent to perform manipulations to the dom during iteration, see below (hope it helps :D), -nathan *a dot buffa at sns dot it* 29-May-2008 04:28 <http://us2.php.net/manual/en/class.domnodelist.php#83513> I agree with drichter at muvicom dot de. For istance, in order to delete each child node of a particular parent node, <?php while ($parentNode->hasChildNodes()){ $domNodeList = $parentNode->childNodes; $parentNode->removeChild($domNodeList->item(0)); } ?> In other word you have to uptade the DomNodeList on every iteration. In my opinion, the DomNodeList class is useless.