Req #38489 [Opn->Csd]: DOMNodeList should implement Traversable

From: Date: Tue, 10 Jan 2012 16:42:53 +0000
Subject: Req #38489 [Opn->Csd]: DOMNodeList should implement Traversable
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-166327@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=38489&edit=1

 ID:                 38489
 Updated by:         jpauli@php.net
 Reported by:        mmcintyre at squiz dot net
 Summary:            DOMNodeList should implement Traversable
-Status:             Open
+Status:             Closed
 Type:               Feature/Change Request
 Package:            DOM XML related
 Operating System:   *
 PHP Version:        5.1.5
-Assigned To:        
+Assigned To:        jpauli
 Block user comment: N
 Private report:     N

 New Comment:

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php

DomNodeListe implements Traversable. You can convert a Traversable object to an 
Iterator using the IteratorIterator 
class.

<?php
$xml = '<queries><query
attr1="value"></query></queries>';

$doc = new DOMDocument;
$doc->loadXML($xml);

$queries = $doc->getElementsByTagName('queries');
$nodes   = iterator_to_array(new IteratorIterator($queries));
?>


Previous Comments:
------------------------------------------------------------------------
[2007-01-17 15:11:55] jules_papillon_fh at yahoo dot de

This bug exists further reproducibly on PHP5.2

------------------------------------------------------------------------
[2007-01-17 15:09:11] jules_papillon_fh at yahoo dot de

Another Code to reproduce the Bug:
----------------------------------
$dom = new DOMDocument('1.0', 'ISO-8859-1');
$dom->load('file.xml');
$iterator = new RecursiveIteratorIterator($dom->childNodes,
RecursiveIteratorIterator::SELF_FIRST);
 
foreach($iterator as $name => $element) {
  print $name . "\n";
}

Expected result:
----------------
A recursive List of all Elements

Actual result:
--------------
"Catchable fatal error:  Argument 1 passed to RecursiveIteratorIterator::__construct() must
implement interface Traversable, instance of DOMNodeList given, called in […] and defined in
[…]"

------------------------------------------------------------------------
[2006-08-18 06:37:22] mmcintyre at squiz dot net

Description:
------------
Currently, a DOMNodeList object can be traversed using a foreach loop, but it cannot be converted to
an array using iterator_to_array(), as it produces an error "Warning: iterator_to_array()
expects parameter 1 to be Traversable, object given"

Reproduce code:
---------------
$xml = '<queries><query
attr1="value"></query></queries>';

$doc = new DOMDocument;
$doc->loadXML($xml);

$queries = $doc->getElementsByTagName('queries');
$nodes   = iterator_to_array($queries);

Expected result:
----------------
The nodes in the NodeList are returned as an array.

Actual result:
--------------
"Warning: iterator_to_array() expects parameter 1 to be Traversable, object given"


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=38489&edit=1


Thread (1 message)

  • jpauli@php.net
  • Unknown Message
    • jpauli@php.net
« previous php.bugs (#166327) next »