|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
)
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 19:00:01 2025 UTC |
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.