Zend_Json-xml2json.xml 4.8 KB

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