php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46048 SimpleXML top-level @attributes not part of iterator (PHP_5_3 and above only!)
Submitted: 2008-09-10 23:58 UTC Modified: 2009-02-04 00:20 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at isnoop dot net Assigned: davidc
Status: Closed Package: SimpleXML related
PHP Version: 5.3.0alpha3 OS: Ubuntu 8.04 server
Private report: No CVE-ID:
 [2008-09-10 23:58 UTC] php at isnoop dot net
Description:
------------
The @attributes for the top-level element in a SimpleXMLElement object aren't returned as part of any iteration over the object.

Reproduced in 5.3.0alpha1 as well.

Reproduce code:
---------------
$xml = '
<data id="1">
	<key>value</name>
</data>
';
$obj = simplexml_load_string($xml);
print_r(get_object_vars($obj));


Expected result:
----------------
Array
(   
    [@attributes] => Array
        (   
            [id] => 1
        )
    [key] => value
)



Actual result:
--------------
Array
(   
    [key] => value
)



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-11 10:50 UTC] jani@php.net
Your xml is invalid:

$ php -n t.php

Warning: simplexml_load_string(): Entity: line 3: parser error : Opening and ending tag mismatch: key line 3 and name in /home/jani/src/build/php_5_3_tst/t.php on line 8

Warning: simplexml_load_string(): <key>value</name> in /home/jani/src/build/php_5_3_tst/t.php on line 8

Warning: simplexml_load_string():                  ^ in /home/jani/src/build/php_5_3_tst/t.php on line 8


With non-invalid XML I get expected result.

 [2008-11-11 21:51 UTC] php at isnoop dot net
Thank you for that.  Reproduce code should be as follows:
$xml = '
<data id="1">
	<key>value</key>
</data>
';
$obj = simplexml_load_string($xml);
print_r(get_object_vars($obj));
 [2008-11-17 09:49 UTC] jani@php.net
$ php t.php
Array
(
    [@attributes] => Array
        (
            [id] => 1
        )

    [key] => value
)
[jani@localhost ~]$ php -v
PHP 5.2.7-dev (cli) (built: Oct  9 2008 16:54:00) (DEBUG)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies

So still no bug. If you experience this even with proper XML, I guess it's your libxml that is a buggy version. (my PHP is build with libxml 2.7.2)
 [2008-11-17 21:27 UTC] php at isnoop dot net
It appears that you're testing against 5.2.7 which I've verified doesn't exhibit this error.  This problem only appears with the 5.3.0 releases.

Both the 5.2.* releases and 5.3.* use libxml 2.6.31 on my primary test machine.  It is possible that libxml is part of the problem, but it still stands that the problem does not manifest prior to 5.3.
 [2008-12-09 01:51 UTC] php at isnoop dot net
I have tested this with the two new PHP releases on yet another server:

5.3.0alpha3 Fails:
[] [ian@ianm:~/src/php-5.3.0alpha3/sapi/cli] ./php -v;./php ~/sandbox/bug46048.php 
PHP 5.3.0alpha3 (cli) (built: Dec  4 2008 17:18:52) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2008 Zend Technologies
Array
(
    [key] => value
)



5.2.8 Passes:
[] [ian@ianm:~/src/php-5.2.8/sapi/cli] ./php -v;./php ~/sandbox/bug46048.php 
PHP 5.2.8 (cli) (built: Dec  8 2008 17:34:28) 
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
Array
(
    [@attributes] => Array
        (
            [id] => 1
        )
    [key] => value
)
 [2009-01-16 20:16 UTC] php at zjs dot name
I am experiencing the same problem on 5.3. The code used is the same used above.

shephezj@ursa-minor:~/php5/sapi/cli$ ./php -v; ./php ~/code/scrap/php/bugs/46048.php
PHP 5.3.0alpha4-dev (cli) (built: Jan 15 2009 17:22:45) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies
Array
(
    [key] => value
)
 [2009-01-29 22:45 UTC] php at isnoop dot net
Bug confirmed still be present in 5.3.0beta1.
 [2009-02-03 21:46 UTC] davidc@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2009-02-04 00:20 UTC] php at isnoop dot net
Thank you!  Confirmed fixed in latest CVS.
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 07:02:14 2014 UTC