<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.json.xml2json">
    <title>XML to JSON conversion</title>

    <para>
        <classname>Zend_Json</classname> provides a convenience method for transforming
        <acronym>XML</acronym> formatted data into <acronym>JSON</acronym> format. This feature was
        inspired from an <ulink url="http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/">
        IBM developerWorks article</ulink>.
    </para>

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

    <programlisting language="php"><![CDATA[
// fromXml function simply takes a String containing XML contents
// as input.
$jsonContents = Zend_Json::fromXml($xmlStringContents, true);
]]></programlisting>

    <para>
        <methodname>Zend_Json::fromXml()</methodname> function does the conversion of the
        <acronym>XML</acronym> formatted string input parameter and returns the equivalent
        <acronym>JSON</acronym> formatted string output. In case of any <acronym>XML</acronym> input
        format error or conversion logic error, this function will throw an exception. The
        conversion logic also uses recursive techniques to traverse the <acronym>XML</acronym> tree.
        It supports recursion upto 25 levels deep. Beyond that depth, it will throw a
        <classname>Zend_Json_Exception</classname>. There are several <acronym>XML</acronym> 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.
    </para>

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

    <para>
        <acronym>XML</acronym> input string passed to <methodname>Zend_Json::fromXml()</methodname>
        function:
    </para>

    <programlisting language="php"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book id="1">
        <title>Code Generation in Action</title>
        <author><first>Jack</first><last>Herrington</last></author>
        <publisher>Manning</publisher>
    </book>

    <book id="2">
        <title>PHP Hacks</title>
        <author><first>Jack</first><last>Herrington</last></author>
        <publisher>O'Reilly</publisher>
    </book>

    <book id="3">
        <title>Podcasting Hacks</title>
        <author><first>Jack</first><last>Herrington</last></author>
        <publisher>O'Reilly</publisher>
    </book>
</books>
]]></programlisting>

    <para>
        <acronym>JSON</acronym> output string returned from
        <methodname>Zend_Json::fromXml()</methodname> function:
    </para>

    <programlisting language="php"><![CDATA[
{
   "books" : {
      "book" : [ {
         "@attributes" : {
            "id" : "1"
         },
         "title" : "Code Generation in Action",
         "author" : {
            "first" : "Jack", "last" : "Herrington"
         },
         "publisher" : "Manning"
      }, {
         "@attributes" : {
            "id" : "2"
         },
         "title" : "PHP Hacks", "author" : {
            "first" : "Jack", "last" : "Herrington"
         },
         "publisher" : "O'Reilly"
      }, {
         "@attributes" : {
            "id" : "3"
         },
         "title" : "Podcasting Hacks", "author" : {
            "first" : "Jack", "last" : "Herrington"
         },
         "publisher" : "O'Reilly"
      }
   ]}
}
]]></programlisting>

<sect2 id="zend.json.xml2json.changes">
    <title>Changes</title>

    <sect3 id="zend.json.xml2json.changes.1-11-6">
        <title>Changes in 1.11.6</title>

        <para>
            Starting from the release 1.11.6 the <methodname>Zend_Json::fromXml()</methodname> function
            has been rewritten from scratch in order to manage XML element with attributes, text value
            and sub-elements (see the <ulink url="http://framework.zend.com/issues/browse/ZF-3257">ZF-3257</ulink>).
        </para>

        <para>
            For instance, if you have an XML document like this:
        </para>

        <programlisting language="php"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<a>
    <b id="foo"/>
    bar
</a>
]]></programlisting>

            <para>
                The <acronym>JSON</acronym> output string returned from
                <methodname>Zend_Json::fromXml()</methodname> is:
            </para>

            <programlisting language="php"><![CDATA[
{
   "a" : {
      "b" : {
         "@attributes" : {
            "id" : "foo"
         }
      },
      "@text" : "bar"
   }
}
]]></programlisting>

            <para>
                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:
            </para>

            <programlisting language="php"><![CDATA[
<?xml version="1.0" encoding="UTF-8"?>
<a>foo</a>
]]></programlisting>

            <para>
                the JSON will be {"a":"foo"} that is quite intuitive, instead of {"a":{"@text":"foo"}}.
            </para>
    </sect3>
</sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->
