1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.json.xml2json">
4 <title>XML to JSON conversion</title>
7 <classname>Zend_Json</classname> provides a convenience method for transforming
8 <acronym>XML</acronym> formatted data into <acronym>JSON</acronym> format. This feature was
9 inspired from an <ulink url="http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/">
10 IBM developerWorks article</ulink>.
14 <classname>Zend_Json</classname> includes a static function called
15 <methodname>Zend_Json::fromXml()</methodname>. This function will generate
16 <acronym>JSON</acronym> from a given <acronym>XML</acronym> input. This function takes any
17 arbitrary <acronym>XML</acronym> string as an input parameter. It also takes an optional
18 boolean input parameter to instruct the conversion logic to ignore or not ignore the
19 <acronym>XML</acronym> attributes during the conversion process. If this optional input
20 parameter is not given, then the default behavior is to ignore the <acronym>XML</acronym>
21 attributes. This function call is made as shown below:
24 <programlisting language="php"><![CDATA[
25 // fromXml function simply takes a String containing XML contents
27 $jsonContents = Zend_Json::fromXml($xmlStringContents, true);
31 <methodname>Zend_Json::fromXml()</methodname> function does the conversion of the
32 <acronym>XML</acronym> formatted string input parameter and returns the equivalent
33 <acronym>JSON</acronym> formatted string output. In case of any <acronym>XML</acronym> input
34 format error or conversion logic error, this function will throw an exception. The
35 conversion logic also uses recursive techniques to traverse the <acronym>XML</acronym> tree.
36 It supports recursion upto 25 levels deep. Beyond that depth, it will throw a
37 <classname>Zend_Json_Exception</classname>. There are several <acronym>XML</acronym> files
38 with varying degree of complexity provided in the tests directory of Zend Framework. They
39 can be used to test the functionality of the xml2json feature.
43 The following is a simple example that shows both the <acronym>XML</acronym> input string
44 passed to and the <acronym>JSON</acronym> output string returned as a result from the
45 <methodname>Zend_Json::fromXml()</methodname> function. This example used the optional
46 function parameter as not to ignore the <acronym>XML</acronym> attributes during the
47 conversion. Hence, you can notice that the resulting <acronym>JSON</acronym> string includes
48 a representation of the <acronym>XML</acronym> attributes present in the
49 <acronym>XML</acronym> input string.
53 <acronym>XML</acronym> input string passed to <methodname>Zend_Json::fromXml()</methodname>
57 <programlisting language="php"><![CDATA[
58 <?xml version="1.0" encoding="UTF-8"?>
61 <title>Code Generation in Action</title>
62 <author><first>Jack</first><last>Herrington</last></author>
63 <publisher>Manning</publisher>
67 <title>PHP Hacks</title>
68 <author><first>Jack</first><last>Herrington</last></author>
69 <publisher>O'Reilly</publisher>
73 <title>Podcasting Hacks</title>
74 <author><first>Jack</first><last>Herrington</last></author>
75 <publisher>O'Reilly</publisher>
81 <acronym>JSON</acronym> output string returned from
82 <methodname>Zend_Json::fromXml()</methodname> function:
85 <programlisting language="php"><![CDATA[
92 "title" : "Code Generation in Action",
94 "first" : "Jack", "last" : "Herrington"
96 "publisher" : "Manning"
101 "title" : "PHP Hacks", "author" : {
102 "first" : "Jack", "last" : "Herrington"
104 "publisher" : "O'Reilly"
109 "title" : "Podcasting Hacks", "author" : {
110 "first" : "Jack", "last" : "Herrington"
112 "publisher" : "O'Reilly"
119 More details about this xml2json feature can be found in the original proposal itself.
121 <ulink url="http://tinyurl.com/2tfa8z">Zend_xml2json proposal</ulink>.