Zend_Json-xml2json.xml 4.4 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. XML formatted data into JSON 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 <classname>Zend_Json::fromXml()</classname>.
  13. This function will generate JSON from a given XML input. This function takes any arbitrary
  14. XML 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 XML attributes during the conversion process. If this
  16. optional input parameter is not given, then the default behavior is to ignore the XML 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. <classname>Zend_Json::fromXml()</classname> function does the conversion of the XML formatted string input
  26. parameter and returns the equivalent JSON formatted string output. In case of any XML 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 XML tree. It supports recursion upto 25 levels deep.
  29. Beyond that depth, it will throw a <classname>Zend_Json_Exception</classname>. There are several XML 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 XML input string passed to
  35. and the JSON output string returned as a result from the
  36. <classname>Zend_Json::fromXml()</classname> function. This example used the optional function parameter as
  37. not to ignore the XML attributes during the conversion. Hence, you can notice that the
  38. resulting JSON string includes a representation of the XML attributes present in the XML input string.
  39. </para>
  40. <para>
  41. XML input string passed to <classname>Zend_Json::fromXml()</classname> 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. JSON output string returned from <classname>Zend_Json::fromXml()</classname> 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. -->