Advanced Usage of Zend_Json

XML to JSON conversion

Zend_Json provides a convenience method for transforming XML formatted data into JSON format. This feature was inspired from an » IBM developerWorks article.

Zend_Json includes a static function called Zend_Json::fromXml(). This function will generate JSON from a given XML input. This function takes any arbitrary XML string as an input parameter. It also takes an optional boolean input parameter to instruct the conversion logic to ignore or not ignore the XML attributes during the conversion process. If this optional input parameter is not given, then the default behavior is to ignore the XML attributes. This function call is made as shown below:

  1. // fromXml function simply takes a String containing XML contents
  2. // as input.

Zend_Json::fromXml() function does the conversion of the XML formatted string input parameter and returns the equivalent JSON formatted string output. In case of any XML input format error or conversion logic error, this function will throw an exception. The conversion logic also uses recursive techniques to traverse the XML tree. It supports recursion upto 25 levels deep. Beyond that depth, it will throw a Zend_Json_Exception. There are several XML files with varying degree of complexity provided in the tests directory of Zend Framework. They can be used to test the functionality of the xml2json feature.

The following is a simple example that shows both the XML input string passed to and the JSON output string returned as a result from the Zend_Json::fromXml() function. This example used the optional function parameter as not to ignore the XML attributes during the conversion. Hence, you can notice that the resulting JSON string includes a representation of the XML attributes present in the XML input string.

XML input string passed to Zend_Json::fromXml() function:

  1. span style="color: #ff0000;">"1.0" encoding="UTF-8""1""2"'Reilly</publisher>
  2.     </book>
  3.  
  4.     <book id="3">
  5.         <title>Podcasting Hacks</title>
  6.         <author><first>Jack</first><last>Herrington</last></author>
  7.         <publisher>O'Reilly</publisher>
  8.     </book>
  9. </books>

JSON output string returned from Zend_Json::fromXml() function:

  1. {
  2.    "books" : {
  3.       "book" : [ {
  4.          "@attributes" : {
  5.             "id" : "1"
  6.          },
  7.          "title" : "Code Generation in Action",
  8.          "author" : {
  9.             "first" : "Jack", "last" : "Herrington"
  10.          },
  11.          "publisher" : "Manning"
  12.       }, {
  13.          "@attributes" : {
  14.             "id" : "2"
  15.          },
  16.          "title" : "PHP Hacks", "author" : {
  17.             "first" : "Jack", "last" : "Herrington"
  18.          },
  19.          "publisher" : "O'Reilly"
  20.       }, {
  21.          "@attributes" : {
  22.             "id" : "3"
  23.          },
  24.          "title" : "Podcasting Hacks", "author" : {
  25.             "first" : "Jack", "last" : "Herrington"
  26.          },
  27.          "publisher" : "O'Reilly"
  28.       }
  29.    ]}
  30. }

Changes

Changes in 1.11.6

Starting from the release 1.11.6 the Zend_Json::fromXml() function has been rewritten from scratch in order to manage XML element with attributes, text value and sub-elements (see the » ZF-3257).

For instance, if you have an XML document like this:

  1. span style="color: #ff0000;">"1.0" encoding="UTF-8""foo"/>
  2.     bar
  3. </a>

The JSON output string returned from Zend_Json::fromXml() is:

  1. {
  2.    "a" : {
  3.       "b" : {
  4.          "@attributes" : {
  5.             "id" : "foo"
  6.          }
  7.       },
  8.       "@text" : "bar"
  9.    }
  10. }

The idea is to use a special key value (@text) to store the text value of an XML element, only if this element contains attributes or sub-elements (as in the previous examples). If you have a simple XML element with only a text value, like this:

  1. span style="color: #ff0000;">"1.0" encoding="UTF-8"

the JSON will be {"a":"foo"} that is quite intuitive, instead of {"a":{"@text":"foo"}}.


Advanced Usage of Zend_Json