So I got some XML and thought “well, simplexml looks like an easy way to parse the xml.” Then serialized it with json_encode and json_decode. BUT it loses the attributes on some of the nodes!!!
Someone reported this as a bug:
If you json encode a simplexml object, any xml leaf nodes will lose
The attached script has two small xml strings. One with attributes
on the leaf node, one without.
When you run the script, attribute values disappear for the second
Here’s the reply they got, that it’s not a bug!! Why does it keep some attributes, but not others, why is it not lossless??? If it DOESN’T WORK, why is it available? It’s not a bug because it wasn’t supposed to keep all the attributes, only some, randomly? Defective design doesn’t count as a bug? I don’t get it…
“[20 Oct 2008 11:10pm UTC] firstname.lastname@example.org
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
You cannot do a lossless (loss of attributes) on an object of a certain
type such as SimpleXML.”
You can put simplexml data into session variables by casting it to string first (string), could that be one way of getting around the attribute loss by not having to use json?