Zend_Json-xml2json.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <!-- EN-Revision: 24249 -->
  4. <sect1 id="zend.json.xml2json">
  5. <title>XML から JSON への変換</title>
  6. <para>
  7. <classname>Zend_Json</classname> には、<acronym>XML</acronym> 形式のデータを
  8. <acronym>JSON</acronym> 形式に変換するための便利なメソッドがあります。
  9. この機能は
  10. <ulink url="http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/">
  11. IBM developerWorks の記事</ulink>
  12. に触発されて追加したものです。
  13. </para>
  14. <para>
  15. <classname>Zend_Json</classname> には、静的関数 <methodname>Zend_Json::fromXml()</methodname>
  16. が搭載されています。この関数は、<acronym>XML</acronym> を受け取って <acronym>JSON</acronym> を作成します。
  17. 入力パラメータには、任意の <acronym>XML</acronym> 文字列を渡せます。
  18. また、オプションのパラメータで論理値を渡し、
  19. 変換処理中に <acronym>XML</acronym> の属性を無視するかどうかを指定できます。
  20. このパラメータを省略した場合のデフォルトの挙動は、
  21. <acronym>XML</acronym> の属性を無視します。この関数の使用法は、以下のようになります。
  22. </para>
  23. <programlisting language="php"><![CDATA[
  24. // fromXml 関数の入力には、XML を含む文字列を渡します
  25. $jsonContents = Zend_Json::fromXml($xmlStringContents, true);
  26. ]]></programlisting>
  27. <para>
  28. <methodname>Zend_Json::fromXml()</methodname> 関数は、
  29. 入力として渡された <acronym>XML</acronym> 形式の文字列を、
  30. それと同等な <acronym>JSON</acronym> 形式の文字列に変換して返します。
  31. <acronym>XML</acronym> の書式にエラーがあったり変換中にエラーが発生した場合は、
  32. この関数は例外をスローします。
  33. 変換ロジックは、<acronym>XML</acronym> ツリーを再帰的に走査します。
  34. 最大で 25 段階までの再帰を行います。
  35. それ以上の深さに達した場合は <classname>Zend_Json_Exception</classname>
  36. をスローします。Zend Framework の
  37. tests ディレクトリ内には <acronym>XML</acronym> ファイルがいくつか入っているので、
  38. それらを用いると xml2json の機能を確認できます。
  39. </para>
  40. <para>
  41. <acronym>XML</acronym> 入力文字列の例と、<methodname>Zend_Json::fromXml()</methodname>
  42. 関数が返す <acronym>JSON</acronym> 文字列の例を次に示します。
  43. この例では、オプションのパラメータで
  44. <acronym>XML</acronym> の属性を無視しないように設定しています。
  45. そのため、<acronym>JSON</acronym> 文字列の中に <acronym>XML</acronym>
  46. の属性の情報が含まれていることがわかるでしょう。
  47. </para>
  48. <para>
  49. <methodname>Zend_Json::fromXml()</methodname> 関数に渡す <acronym>XML</acronym> 入力文字列です。
  50. </para>
  51. <programlisting language="php"><![CDATA[
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <books>
  54. <book id="1">
  55. <title>Code Generation in Action</title>
  56. <author><first>Jack</first><last>Herrington</last></author>
  57. <publisher>Manning</publisher>
  58. </book>
  59. <book id="2">
  60. <title>PHP Hacks</title>
  61. <author><first>Jack</first><last>Herrington</last></author>
  62. <publisher>O'Reilly</publisher>
  63. </book>
  64. <book id="3">
  65. <title>Podcasting Hacks</title>
  66. <author><first>Jack</first><last>Herrington</last></author>
  67. <publisher>O'Reilly</publisher>
  68. </book>
  69. </books>
  70. ]]></programlisting>
  71. <para>
  72. <methodname>Zend_Json::fromXml()</methodname> 関数が返す <acronym>JSON</acronym> 文字列です。
  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>変更内容</title>
  108. <sect3 id="zend.json.xml2json.changes.1-11-6">
  109. <title>1.11.6 での変更内容</title>
  110. <!-- TODO : to be translated -->
  111. <para>
  112. Starting from the release 1.11.6 the <methodname>Zend_Json::fromXml()</methodname> function
  113. has been rewritten from scratch in order to manage XML element with attributes, text value
  114. and sub-elements (see the <ulink url="http://framework.zend.com/issues/browse/ZF-3257">ZF-3257</ulink>).
  115. </para>
  116. <para>
  117. For instance, if you have an XML document like this:
  118. </para>
  119. <programlisting language="php"><![CDATA[
  120. <?xml version="1.0" encoding="UTF-8"?>
  121. <a>
  122. <b id="foo"/>
  123. bar
  124. </a>
  125. ]]></programlisting>
  126. <para>
  127. <methodname>Zend_Json::fromXml()</methodname> から返される
  128. <acronym>JSON</acronym> 出力文字列はこうです。
  129. </para>
  130. <programlisting language="php"><![CDATA[
  131. {
  132. "a" : {
  133. "b" : {
  134. "@attributes" : {
  135. "id" : "foo"
  136. }
  137. },
  138. "@text" : "bar"
  139. }
  140. }
  141. ]]></programlisting>
  142. <!-- TODO : to be translated -->
  143. <para>
  144. The idea is to use a special key value (@text) to store the text value of an XML element,
  145. only if this element contains attributes or sub-elements (as in the previous examples).
  146. If you have a simple XML element with only a text value, like this:
  147. </para>
  148. <programlisting language="php"><![CDATA[
  149. <?xml version="1.0" encoding="UTF-8"?>
  150. <a>foo</a>
  151. ]]></programlisting>
  152. <para>
  153. the JSON will be {"a":"foo"} that is quite intuitive, instead of {"a":{"@text":"foo"}}.
  154. </para>
  155. </sect3>
  156. </sect2>
  157. </sect1>
  158. <!--
  159. vim:se ts=4 sw=4 et:
  160. -->