Bug #52463 [Com]: SimpleXml ignores attributes

From: Date: Mon, 26 Jan 2015 20:56:32 +0000
Subject: Bug #52463 [Com]: SimpleXml ignores attributes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190233@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=52463&edit=1 ID: 52463 Comment by: jake dot e dot wilson at gmail dot com Reported by: circus2 at freenet dot de Summary: SimpleXml ignores attributes Status: Duplicate Type: Bug Package: SimpleXML related Operating System: Win7 Home Premium GER PHP Version: 5.3.3 Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2014-04-03 17:30:34] 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>')); ------------------------------------------------------------------------ [2014-04-02 15:55:47] ab@php.net Related To: Bug #66084 ------------------------------------------------------------------------ [2014-04-02 12:15:43] zashme at gmail dot com I have the same problem, i have PHP 5.5.10 x86_64, linux 3.13.6 ------------------------------------------------------------------------ [2013-06-19 07:27:46] 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); ------------------------------------------------------------------------ [2010-07-28 07:14:28] 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))); ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=52463 -- Edit this bug report at https://bugs.php.net/bug.php?id=52463&edit=1

« previous php.bugs (#190233) next »