开发者

Remove all children from a XML Node PHP DOM

开发者 https://www.devze.com 2023-01-14 09:56 出处:网络
I want to remove all children from a开发者_开发百科 XML Node using PHP DOM, is there any difference between:

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


Slightly tighter:

  while ($parentNode->hasChildNodes()) {
    $parentNode->removeChild($parentNode->firstChild);
  }


removeChild() is the more "proper" way of doing things. While you can set the contents of that node to "" and this will acquire the desired effect, calling removeChild() is much more apparent as to what is going on. However, it would be my assumption that, in a minuscule level, nodeValue() is slightly more efficient.

0

精彩评论

暂无评论...
验证码 换一张
取 消