ZF-3257: Attributes or character data lost in Zend_Json::fromXml() in specific circumstances

Issue Type: Bug Created: 2008-05-14T01:43:34.000+0000 Last Updated: 2011-08-26T23:38:02.000+0000 Status: Resolved Fix version(s): - 1.11.7 (02/Jun/11)

Reporter: Ciaran McNulty (ciaranmcnulty) Assignee: Enrico Zimuel (zimuel) Tags: - Zend_Json

Related issues: - ZF-5417



Example 1 - loss of character data

When a node's children are a mixture of character data and xml nodes, the character data is lost

$xml = '****bar'; echo Zend_Json::fromXml($xml, false);

Output: {"a":{"b":{"@attributes":{"id":"foo"

Example 2 - loss of attribute data

When a node has a single character data child, any attributes are lost

$xml = 'bar'; echo Zend_Json::fromXml($xml, false);

Output: {"a":{"b":"bar"


Posted by Wil Sinclair (wil) on 2008-06-09T12:38:44.000+0000

Please evaluate and fix/categorize as necessary.

Posted by Eric Johnson (edjoon) on 2008-11-21T08:53:18.000+0000

What's the status on this? Seems like a pretty major problem.

Posted by Stanislav Malyshev (stas) on 2009-06-17T15:33:51.000+0000

What output would you expect in this case?

Posted by Chris Edwards (spcmky) on 2009-10-13T17:18:56.000+0000

I dunno what the output should be, however I would suspect that the conversion should not loose any data. This is a pretty major issue and there needs to be a resolution.

Posted by Matthew Weier O'Phinney (matthew) on 2009-10-14T05:10:20.000+0000

Stas: In Example 1, note that the value of the "b" element, "bar", is not in the JSON representation. In Example2, the attribute "id" found in element "b" is not represented in the JSON.

I'm not entirely sure how you represent an XML element that has both attributes and values, but surely we need to be able to do so.

Posted by Chris Edwards (spcmky) on 2009-10-14T09:15:44.000+0000

One example would be to create a special array index called "name" or"value" or etc. that would contain the element's info. This way it can coexist with the attributes index. This changes the data structure though but it's a special case and would need to be handled when converting back to xml.

Posted by Enrico Zimuel (zimuel) on 2011-04-29T07:34:44.000+0000

My suggestion is to store the text value of a XML element using the key "@text" in the JSON format. The idea is to use the same logic of the @attributes JSON element. For instance, the following XML: ****bar can be represented in this JSON structure: {"a":{"b":{"@attributes":{"id":"foo","@text":"bar" This should be the behaviour only in presence of "complex" XML elements (that means elements with attributes or sub-elements). For instance the following simple XML: foo will continue to be represented with the JSON string: {"a":"foo"}, instead of {"a":{"@text":"foo"

Posted by Enrico Zimuel (zimuel) on 2011-05-03T16:35:23.000+0000

Resolved in branches/release-1.11 (commit 23971 and 23973)

Posted by Simon Stücher (billomat) on 2011-05-16T15:26:20.000+0000

With your changes in rev23915 $recursionDepth from _processXml is not recursion depth but the number of root-child-elements! Therefore you are not able to process a xml with more than 25 elements! The old one hat a additional $recursionDepth--; in line 331 which is missing in revision 23915

Posted by Enrico Zimuel (zimuel) on 2011-05-16T16:42:18.000+0000

Simon thanks for the feedback. I used a $recursionDepth++ instead of $recursionDepth+1 in the recursive call (line 280). Resolved in trunk (commit 24039). Please test it and let me know, thanks.

Posted by Simon Stücher (billomat) on 2011-05-17T08:08:58.000+0000

Thanks for the very quick response and bugfix, Enrico! Now it works as expected.

Posted by Enrico Zimuel (zimuel) on 2011-05-17T09:05:40.000+0000

Resolved in branches/release-1.11 (commit 24040)

Posted by Adam Lundrigan (adamlundrigan) on 2011-08-26T23:38:02.000+0000

Issued ZF2 pull request PR-356 to have changes pulled forward

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.