|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-10-09 19:17 UTC] josh at coady dot us
Description:
------------
DOMNode should have a property like $exists to check if the node still exists. This would be useful to avoid the "Node no longer exists" warning.
We should be able to do something like
$node->parentNode->removeChild($node);
if($node->exists)
{
// use node ..
}
Reproduce code:
---------------
$doc = DOMDocument::loadXML('<doc><x><a>1</a></x><x><a>1</a><y><a>1</a><a>0</a></y></x></doc>');
$nodes = $doc->getElementsByTagName('a');
$nodesToRemove = array();
foreach($nodes as $node)
{
if($node->nodeValue == 1)
{
$nodesToRemove[] = $node;
$node->parentNode->removeChild($node);
}
}
foreach($nodesToRemove as $node)
{
$node->parentNode->removeChild($node);
}
Expected result:
----------------
This is more of a feature request. I expect to change the last loop to something like
foreach($nodesToRemove as $node)
{
if($node->exists)
{
$node->parentNode->removeChild($node);
}
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 15:00:01 2025 UTC |
reproduce code should be the following (I realized the code wasnt duplicating the exact situation I was encountering, so I modified it to better match) $doc = DOMDocument::loadXML('<doc><x><a>1</a></x><x><a>1</a><y><a>1</a><a>0</a></y></x></doc>'); $nodes = $doc->getElementsByTagName('a'); $nodesToRemove = array(); foreach($nodes as $node) { if($node->nodeValue == 1) { $nodesToRemove[] = $node; } } foreach($nodesToRemove as $node) { $node->parentNode->parentNode->removeChild($node->parentNode); }