Zend_Json-xml2json.xml 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.json.xml2json">
  4. <title>XML to JSON conversion</title>
  5. <para>
  6. <classname>Zend_Json</classname> provides a convenience method for transforming
  7. <acronym>XML</acronym> formatted data into <acronym>JSON</acronym> format. This feature was inspired from an
  8. <ulink url="http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/">
  9. IBM developerWorks article</ulink>.
  10. </para>
  11. <para>
  12. <classname>Zend_Json</classname> includes a static function called <methodname>Zend_Json::fromXml()</methodname>.
  13. This function will generate <acronym>JSON</acronym> from a given <acronym>XML</acronym> input. This function takes any arbitrary
  14. <acronym>XML</acronym> string as an input parameter. It also takes an optional boolean input parameter to instruct the
  15. conversion logic to ignore or not ignore the <acronym>XML</acronym> attributes during the conversion process. If this
  16. optional input parameter is not given, then the default behavior is to ignore the <acronym>XML</acronym> attributes.
  17. This function call is made as shown below:
  18. </para>
  19. <programlisting language="php"><![CDATA[
  20. // fromXml function simply takes a String containing XML contents
  21. // as input.
  22. $jsonContents = Zend_Json::fromXml($xmlStringContents, true);
  23. ]]></programlisting>
  24. <para>
  25. <methodname>Zend_Json::fromXml()</methodname> function does the conversion of the <acronym>XML</acronym> formatted string input
  26. parameter and returns the equivalent <acronym>JSON</acronym> formatted string output. In case of any <acronym>XML</acronym> input format
  27. error or conversion logic error, this function will throw an exception. The conversion logic also
  28. uses recursive techniques to traverse the <acronym>XML</acronym> tree. It supports recursion upto 25 levels deep.
  29. Beyond that depth, it will throw a <classname>Zend_Json_Exception</classname>. There are several <acronym>XML</acronym> files with varying
  30. degree of complexity provided in the tests directory of Zend Framework. They can be used to
  31. test the functionality of the xml2json feature.
  32. </para>
  33. <para>
  34. The following is a simple example that shows both the <acronym>XML</acronym> input string passed to
  35. and the <acronym>JSON</acronym> output string returned as a result from the
  36. <methodname>Zend_Json::fromXml()</methodname> function. This example used the optional function parameter as
  37. not to ignore the <acronym>XML</acronym> attributes during the conversion. Hence, you can notice that the
  38. resulting <acronym>JSON</acronym> string includes a representation of the <acronym>XML</acronym> attributes present in the <acronym>XML</acronym> input string.
  39. </para>
  40. <para>
  41. <acronym>XML</acronym> input string passed to <methodname>Zend_Json::fromXml()</methodname> function:
  42. </para>
  43. <programlisting language="php"><![CDATA[
  44. <?xml version="1.0" encoding="UTF-8"?>
  45. <books>
  46. <book id="1">
  47. <title>Code Generation in Action</title>
  48. <author><first>Jack</first><last>Herrington</last></author>
  49. <publisher>Manning</publisher>
  50. </book>
  51. <book id="2">
  52. <title>PHP Hacks</title>
  53. <author><first>Jack</first><last>Herrington</last></author>
  54. <publisher>O'Reilly</publisher>
  55. </book>
  56. <book id="3">
  57. <title>Podcasting Hacks</title>
  58. <author><first>Jack</first><last>Herrington</last></author>
  59. <publisher>O'Reilly</publisher>
  60. </book>
  61. </books>
  62. ]]></programlisting>
  63. <para>
  64. <acronym>JSON</acronym> output string returned from <methodname>Zend_Json::fromXml()</methodname> function:
  65. </para>
  66. <programlisting language="php"><![CDATA[
  67. {
  68. "books" : {
  69. "book" : [ {
  70. "@attributes" : {
  71. "id" : "1"
  72. },
  73. "title" : "Code Generation in Action",
  74. "author" : {
  75. "first" : "Jack", "last" : "Herrington"
  76. },
  77. "publisher" : "Manning"
  78. }, {
  79. "@attributes" : {
  80. "id" : "2"
  81. },
  82. "title" : "PHP Hacks", "author" : {
  83. "first" : "Jack", "last" : "Herrington"
  84. },
  85. "publisher" : "O'Reilly"
  86. }, {
  87. "@attributes" : {
  88. "id" : "3"
  89. },
  90. "title" : "Podcasting Hacks", "author" : {
  91. "first" : "Jack", "last" : "Herrington"
  92. },
  93. "publisher" : "O'Reilly"
  94. }
  95. ]}
  96. }
  97. ]]></programlisting>
  98. <para>
  99. More details about this xml2json feature can be found in the original proposal itself.
  100. Take a look at the
  101. <ulink url="http://tinyurl.com/2tfa8z">Zend_xml2json proposal</ulink>.
  102. </para>
  103. </sect1>
  104. <!--
  105. vim:se ts=4 sw=4 et:
  106. -->