php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46265 DOMNode should have a property that checks if it exists
Submitted: 2008-10-09 19:17 UTC Modified: 2008-10-14 06:36 UTC
From: josh at coady dot us Assigned:
Status: Wont fix Package: DOM XML related
PHP Version: 5.2.6 OS: ubuntu 8.04.1
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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);
  }
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-09 19:23 UTC] josh at coady dot us
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);
}
 [2008-10-14 06:36 UTC] chregu@php.net
Just check, if it has a parentNode. If it has, it does exist, if it 
doesn't, it does not exist in the tree.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Oct 26 02:03:38 2021 UTC