Zend_Json-xml2json.xml 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. <sect2 id="zend.json.xml2json.changes">
  107. <title>Changes</title>
  108. <sect3 id="zend.json.xml2json.changes.1-11-6">
  109. <title>Changes in 1.11.6</title>
  110. <para>
  111. Starting from the release 1.11.6 the <methodname>Zend_Json::fromXml()</methodname> function
  112. has been rewritten from scratch in order to manage XML element with attributes, text value
  113. and sub-elements (see the <ulink url="http://framework.zend.com/issues/browse/ZF-3257">ZF-3257</ulink>).
  114. </para>
  115. <para>
  116. For instance, if you have an XML document like this:
  117. </para>
  118. <programlisting language="php"><![CDATA[
  119. <?xml version="1.0" encoding="UTF-8"?>
  120. <a>
  121. <b id="foo"/>
  122. bar
  123. </a>
  124. ]]></programlisting>
  125. <para>
  126. The <acronym>JSON</acronym> output string returned from
  127. <methodname>Zend_Json::fromXml()</methodname> is:
  128. </para>
  129. <programlisting language="php"><![CDATA[
  130. {
  131. "a" : {
  132. "b" : {
  133. "@attributes" : {
  134. "id" : "foo"
  135. }
  136. },
  137. "@text" : "bar"
  138. }
  139. }
  140. ]]></programlisting>
  141. <para>
  142. The idea is to use a special key value (@text) to store the text value of an XML element,
  143. only if this element contains attributes or sub-elements (as in the previous examples).
  144. If you have a simple XML element with only a text value, like this:
  145. </para>
  146. <programlisting language="php"><![CDATA[
  147. <?xml version="1.0" encoding="UTF-8"?>
  148. <a>foo</a>
  149. ]]></programlisting>
  150. <para>
  151. the JSON will be {"a":"foo"} that is quite intuitive, instead of {"a":{"@text":"foo"}}.
  152. </para>
  153. </sect3>
  154. </sect2>
  155. </sect1>
  156. <!--
  157. vim:se ts=4 sw=4 et:
  158. -->