Lossless JSON encoding/decoding of XML in PHP????

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
their attributes

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
json string”

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] iliaa@php.net

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?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: