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
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.
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.
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.
Slightly tighter:
while ($parentNode->hasChildNodes()) {
$parentNode->removeChild($parentNode->firstChild);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With