php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38489 DOMNodeList should implement Traversable
Submitted: 2006-08-18 06:37 UTC Modified: 2012-01-10 16:42 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:4 of 5 (80.0%)
Same Version:1 (25.0%)
Same OS:2 (50.0%)
From: mmcintyre at squiz dot net Assigned: jpauli (profile)
Status: Closed Package: DOM XML related
PHP Version: 5.1.5 OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: mmcintyre at squiz dot net
New email:
PHP Version: OS:

 

 [2006-08-18 06:37 UTC] 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"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-17 15:09 UTC] 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 [?]"
 [2007-01-17 15:11 UTC] jules_papillon_fh at yahoo dot de
This bug exists further reproducibly on PHP5.2
 [2011-02-21 21:23 UTC] jani@php.net
-Package: Feature/Change Request +Package: DOM XML related
 [2012-01-10 16:42 UTC] jpauli@php.net
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));
?>
 [2012-01-10 16:42 UTC] jpauli@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: jpauli
 [2013-06-11 08:38 UTC] hanskrentel at yahoo dot de
iterator_to_array() accepts any terversable, therefore there is no need to wrap 
the DOMNodeList into an IteratorIterator first, it can be converted to array 
*directly*

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

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

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

The key used per each node is some kind of ID (positive, long integer) and it 
seems to be unique. Using an array can be useful because a DOMNodeList 
automatically re-orders if nodes therein are deleted. This is not the case with 
the array (naturally).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 06:01:30 2024 UTC