php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31071 DOMNodeList->item(int index) always returns first element
Submitted: 2004-12-13 15:33 UTC Modified: 2004-12-23 17:30 UTC
From: claudio dot bertoli at gmail dot com Assigned:
Status: Not a bug Package: DOM XML related
PHP Version: 5.0.2 OS: Linux (not checked on other)
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: claudio dot bertoli at gmail dot com
New email:
PHP Version: OS:

 

 [2004-12-13 15:33 UTC] claudio dot bertoli at gmail dot com
Description:
------------
DOMNodeList->item(int index) always returns first element in the list.
I tried to cycle over childNodes, using a for cycle: it finds the correct child count, but always gets first item!

I tried on this xml:
<?xml version="1.0"?>
<root><child 
id="1">value 1</child><child 
id="2">value 2</child><child 
id="3">value 3</child><child 
id="4">value 4</child></root>

Reproduce code:
---------------
<pre><?php
	$domdoc = new DOMDocument();
	//$domdoc->
	$domdoc->load("test.xml");
	$domdoc->normalize();
	$root = $domdoc->getElementsByTagName("root")->item(0);
	echo wordwrap(htmlentities($domdoc->saveXML()))."\n------\n";
	$nodeList = $root->childNodes;
	$childrenCount = $nodeList->length;
	echo "\tchildren count: ".$childrenCount."\n";
	for ($i = 0; $i < $childrenCount; $i++)
	{
		$_tmp_node = $nodeList->item($i);
		echo $i."\t";
		echo $_tmp_node->tagName."\t";
		echo $_tmp_node->getAttribute("id")."\t";
		echo $_tmp_node->nodeValue."\n";
	}
?></pre>


Expected result:
----------------
<?xml version="1.0"?>
<root><child id="1">value 1</child><child
id="2">value 2</child><child
id="3">value 3</child><child
id="4">value 4</child></root>

------
	children count: 4
0	child	1	value 1
1	child	2	value 2
2	child	3	value 3
3	child	4	value 4

Actual result:
--------------
<?xml version="1.0"?>
<root><child id="1">value 1</child><child
id="2">value 2</child><child
id="3">value 3</child><child
id="4">value 4</child></root>

------
	children count: 4
0	child	1	value 1
1	child	1	value 1
2	child	1	value 1
3	child	1	value 1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-14 10:21 UTC] chregu@php.net
Works for me

which libxml2 version are you using?


 [2004-12-20 08:55 UTC] claudio dot bertoli at gmail dot com
sorry, back to work only today ^^;

from phpinfo():
libxml Version 	2.5.10
 [2004-12-23 15:54 UTC] claudio dot bertoli at gmail dot com
ok, updating libxml version to 2.6.16 fixed the problem: it was not a php bug! :)
 [2004-12-23 17:30 UTC] chregu@php.net
libxml2 error...
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 13:01:33 2025 UTC