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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
19 + 26 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 22:01:26 2024 UTC