php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52463 SimpleXml ignores attributes
Submitted: 2010-07-27 21:26 UTC Modified: 2010-07-28 07:14 UTC
From: circus2 at freenet dot de Assigned:
Status: Duplicate Package: SimpleXML related
PHP Version: 5.3.3 OS: Win7 Home Premium GER
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: circus2 at freenet dot de
New email:
PHP Version: OS:

 

 [2010-07-27 21:26 UTC] circus2 at freenet dot de
Description:
------------
I have a short xml and the attributes for the second node are not available.



Test script:
---------------
$xml = '
    <xml>
        <value type="string" key="name"></value>
        <value type="string" key="name">myValue</value>
    </xml>
';

var_dump(simplexml_load_string($xml));

Expected result:
----------------
Array
(
    [value] => Array
        (
            [0] => Array
                (
                    [@attributes] => Array
                        (
                            [type] => string
                            [key] => name
                        )

                )

            [1] => Array
                (
                    [@attributes] => Array
                        (
                            [type] => string
                            [key] => name
                        )
                    [0] => myValue

                )
        )

)

Or something similar.


Actual result:
--------------
Array
(
    [value] => Array
        (
            [0] => Array
                (
                    [@attributes] => Array
                        (
                            [type] => string
                            [key] => name
                        )

                )

            [1] => myValue
        )

)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-27 23:10 UTC] rasmus@php.net
-Status: Open +Status: Duplicate
 [2010-07-27 23:10 UTC] rasmus@php.net
This is a known cosmetic issue in var_dump().  If you actually iterate through the 
object it works fine.
 [2010-07-28 07:14 UTC] circus2 at freenet dot de
I'm not concerned about var_dump(). I think more of json_encode() which has the same issues. I use json_encode/json_decode to transform a SimpleXml object to an array.

print_r(json_decode(json_encode($xml), true)));
 [2013-06-19 07:27 UTC] nm at lindenvalley dot de
have same problem, it's not var_dump error, because print_r has same trouble

code:
$xmlObj = simplexml_load_string($xml_string);
print_r ($xmlObj);
 [2014-04-02 12:15 UTC] zashme at gmail dot com
I have the same problem, i have PHP 5.5.10 x86_64, linux 3.13.6
 [2014-04-03 17:30 UTC] php at kenman dot net
As circus2 at freenet dot de points out, this is not merely a var_dump() issue.

Here's a repro from the linked issue (Bug #66084); note the only difference in input is a single space in the XML:

echo json_encode(simplexml_load_string('<a><b/><c><x/></c></a>')), PHP_EOL;
echo json_encode(simplexml_load_string('<a><b/><c> <x/></c></a>'));
 [2015-01-26 20:56 UTC] jake dot e dot wilson at gmail dot com
This is only a bug when printing the values. SimpleXML IS parsing the attributes and you can reference them:

$string = '
<xml>
	<value type="string" key="name"></value>
	<value type="string" key="name">myValue</value>
</xml>
';

$xml = simplexml_load_string($string);

foreach( $xml as $element ) {
	echo $element."\n";
	foreach( $element->attributes() as $key=>$value) {
		echo "\t".$key."=>".$value."\n";
	}
}

will result in


	type=>string
	key=>name
myValue
	type=>string
	key=>name

The attributes (their keys and values) are there. They just don't print out properly when using var_dump, print_r, echo, etc... Just use the ->attributes() method to reference them. However, if this printing/display bug is something to do with SimpleXML it should definitely be fixed. Because this is very misleading to not see the attributes when doing a var_dump.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 08:01:29 2024 UTC