I hava an xml file with the following structure:
<?xml version=”1.0″ encoding=”iso-8859-1″ standalone=”yes”?>
<publications>
<book>
<title>Test Title 1</title>
<author>Test author 1</author>
<year></year>
</book>
<book>
<title>Test Title 2</title>
<author>Test author 2</author>
<year></year>
</book>
</publications>
This xml is loaded into a php object with SimpleXml, converted in a json string and sent to output.
On the client-side, the string is received by a Javascript and converted back to an object using the Prototype function evalJSON. The received string, before the decoding, looks as follows:
{“book”:[{“title”:”Test Title 1″,”author”:”Test author 1″,”year”:{}},{“title”:”Test Title 2″,”author”:”Test author 2″,”year”:{}}]}
All the properties of the re-created object are ok and the property “length” says correctly 2.
Now the problem:
If one node in the original xml is deleted, so that the xml contains now only one node, the whole thing doesn’t work. More exactly, with only one node the received json string loses the square brackets and looks as follows:
{“book”:{“title”:”Test Title 1″,”author”:”Test author 1″,”year”:{}}}
With this structure, after the decoding with evalJSON the object is not correctly recreated and its property length is set to “undefined”.
As a test, I manually re-inserted the square brackets in the correct position of the json string, and everything works again as espected.
Is anyone experiencing the same issue? This creates a lot of problems with the correct view of any photo-gallery, product-catalogue and so on which uses xml as database.
Thanks