PHP: Recursively Process then remove DOMElements from DOMDocument

Lictmeeliclon

New Member
I'm using PHP's DOMDocument and related classes to work with XML. The XML contains processing instructions that must be processed in a specific order, from top to bottom, deepest to shallowest.I'm using a recursive function that takes the entire DOMDocument, then calls itself \[code\]foreach\[/code\] child node, all the way down.I need to delete some of these child nodes after I process them. The problem is that as soon as a child node is deleted, it seems the \[code\]foreach\[/code\] is "out of whack" and I can't delete any more child nodes.Example XML doc:\[code\]<root id="1"> <instruction id="2"> <inner id="3"/> </instruction> <instruction id="4"> <inner id="5"/> </instruction></root>\[/code\]I need to process \[code\]inner[@id=3]\[/code\] first, then delete it. Then I need to process \[code\]instruction[id=2]\[/code\] and delete it. Up until here, everything's fine.Next I need to process \[code\]inner[@id=5]\[/code\] and delete it. I can read/process it ok, but when I try to remove it:\[code\]$parentNode=$inner->parentNode;$parentNode->removeChild($inner);\[/code\]Nothing happens. It seems that removing the first instruction node has made PHP confused about which elements are contained in the document now.I know it's possible to process the XML from bottom to top and remove all of the nodes in that order, but I have a specific need to go from top-to-bottom.One other piece of info here - I'm also adding some new nodes to the document while processing it, in case that changes my options.What do I need to do to get this to work?
 
Back
Top