Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all children from a XML Node PHP DOM

Tags:

dom

php

I want to remove all children from a XML Node using PHP DOM, is there any difference between:

A)

while ($parentNode->hasChildNodes()){
   $parentNode->removeChild($parentNode->childNodes->item(0));
 }

AND

B)

$node->nodeValue = "";

I prefer the second one, seems like I am getting the same result but I'm not sure.

Thanks, Carlos

like image 936
Carlos Dubus Avatar asked Aug 30 '10 19:08

Carlos Dubus


People also ask

How do I remove all children from a DOM element?

Child nodes can be removed from a parent with removeChild(), and a node itself can be removed with remove(). Another method to remove all child of a node is to set it's innerHTML=”” property, it is an empty string which produces the same output.

How do you delete a child in XML?

XML DOM removeChild() Method The removeChild() method removes a specified child node from the current node. Tip: The removed child node can be inserted later into any element in the same document.

How do I uninstall child Dom?

removeChild() The removeChild() method of the Node interface removes a child node from the DOM and returns the removed node. Note: As long as a reference is kept on the removed child, it still exists in memory, but is no longer part of the DOM. It can still be reused later in the code.


1 Answers

Slightly tighter:

  while ($parentNode->hasChildNodes()) {
    $parentNode->removeChild($parentNode->firstChild);
  }
like image 180
dman Avatar answered Oct 05 '22 09:10

dman