| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <!-- EN-Revision: 24249 -->
- <sect1 id="zend.json.xml2json">
- <title>XML から JSON への変換</title>
- <para>
- <classname>Zend_Json</classname> には、<acronym>XML</acronym> 形式のデータを
- <acronym>JSON</acronym> 形式に変換するための便利なメソッドがあります。
- この機能は
- <ulink url="http://www.ibm.com/developerworks/xml/library/x-xml2jsonphp/">
- IBM developerWorks の記事</ulink>
- に触発されて追加したものです。
- </para>
- <para>
- <classname>Zend_Json</classname> には、静的関数 <methodname>Zend_Json::fromXml()</methodname>
- が搭載されています。この関数は、<acronym>XML</acronym> を受け取って <acronym>JSON</acronym> を作成します。
- 入力パラメータには、任意の <acronym>XML</acronym> 文字列を渡せます。
- また、オプションのパラメータで論理値を渡し、
- 変換処理中に <acronym>XML</acronym> の属性を無視するかどうかを指定できます。
- このパラメータを省略した場合のデフォルトの挙動は、
- <acronym>XML</acronym> の属性を無視します。この関数の使用法は、以下のようになります。
- </para>
- <programlisting language="php"><![CDATA[
- // fromXml 関数の入力には、XML を含む文字列を渡します
- $jsonContents = Zend_Json::fromXml($xmlStringContents, true);
- ]]></programlisting>
- <para>
- <methodname>Zend_Json::fromXml()</methodname> 関数は、
- 入力として渡された <acronym>XML</acronym> 形式の文字列を、
- それと同等な <acronym>JSON</acronym> 形式の文字列に変換して返します。
- <acronym>XML</acronym> の書式にエラーがあったり変換中にエラーが発生した場合は、
- この関数は例外をスローします。
- 変換ロジックは、<acronym>XML</acronym> ツリーを再帰的に走査します。
- 最大で 25 段階までの再帰を行います。
- それ以上の深さに達した場合は <classname>Zend_Json_Exception</classname>
- をスローします。Zend Framework の
- tests ディレクトリ内には <acronym>XML</acronym> ファイルがいくつか入っているので、
- それらを用いると xml2json の機能を確認できます。
- </para>
- <para>
- <acronym>XML</acronym> 入力文字列の例と、<methodname>Zend_Json::fromXml()</methodname>
- 関数が返す <acronym>JSON</acronym> 文字列の例を次に示します。
- この例では、オプションのパラメータで
- <acronym>XML</acronym> の属性を無視しないように設定しています。
- そのため、<acronym>JSON</acronym> 文字列の中に <acronym>XML</acronym>
- の属性の情報が含まれていることがわかるでしょう。
- </para>
- <para>
- <methodname>Zend_Json::fromXml()</methodname> 関数に渡す <acronym>XML</acronym> 入力文字列です。
- </para>
- <programlisting language="php"><![CDATA[
- <?xml version="1.0" encoding="UTF-8"?>
- <books>
- <book id="1">
- <title>Code Generation in Action</title>
- <author><first>Jack</first><last>Herrington</last></author>
- <publisher>Manning</publisher>
- </book>
- <book id="2">
- <title>PHP Hacks</title>
- <author><first>Jack</first><last>Herrington</last></author>
- <publisher>O'Reilly</publisher>
- </book>
- <book id="3">
- <title>Podcasting Hacks</title>
- <author><first>Jack</first><last>Herrington</last></author>
- <publisher>O'Reilly</publisher>
- </book>
- </books>
- ]]></programlisting>
- <para>
- <methodname>Zend_Json::fromXml()</methodname> 関数が返す <acronym>JSON</acronym> 文字列です。
- </para>
- <programlisting language="php"><![CDATA[
- {
- "books" : {
- "book" : [ {
- "@attributes" : {
- "id" : "1"
- },
- "title" : "Code Generation in Action",
- "author" : {
- "first" : "Jack", "last" : "Herrington"
- },
- "publisher" : "Manning"
- }, {
- "@attributes" : {
- "id" : "2"
- },
- "title" : "PHP Hacks", "author" : {
- "first" : "Jack", "last" : "Herrington"
- },
- "publisher" : "O'Reilly"
- }, {
- "@attributes" : {
- "id" : "3"
- },
- "title" : "Podcasting Hacks", "author" : {
- "first" : "Jack", "last" : "Herrington"
- },
- "publisher" : "O'Reilly"
- }
- ]}
- }
- ]]></programlisting>
- <sect2 id="zend.json.xml2json.changes">
- <title>変更内容</title>
- <sect3 id="zend.json.xml2json.changes.1-11-6">
- <title>1.11.6 での変更内容</title>
- <!-- TODO : to be translated -->
- <para>
- Starting from the release 1.11.6 the <methodname>Zend_Json::fromXml()</methodname> function
- has been rewritten from scratch in order to manage XML element with attributes, text value
- and sub-elements (see the <ulink url="http://framework.zend.com/issues/browse/ZF-3257">ZF-3257</ulink>).
- </para>
- <para>
- For instance, if you have an XML document like this:
- </para>
- <programlisting language="php"><![CDATA[
- <?xml version="1.0" encoding="UTF-8"?>
- <a>
- <b id="foo"/>
- bar
- </a>
- ]]></programlisting>
- <para>
- <methodname>Zend_Json::fromXml()</methodname> から返される
- <acronym>JSON</acronym> 出力文字列はこうです。
- </para>
- <programlisting language="php"><![CDATA[
- {
- "a" : {
- "b" : {
- "@attributes" : {
- "id" : "foo"
- }
- },
- "@text" : "bar"
- }
- }
- ]]></programlisting>
- <!-- TODO : to be translated -->
- <para>
- The idea is to use a special key value (@text) to store the text value of an XML element,
- only if this element contains attributes or sub-elements (as in the previous examples).
- If you have a simple XML element with only a text value, like this:
- </para>
- <programlisting language="php"><![CDATA[
- <?xml version="1.0" encoding="UTF-8"?>
- <a>foo</a>
- ]]></programlisting>
- <para>
- the JSON will be {"a":"foo"} that is quite intuitive, instead of {"a":{"@text":"foo"}}.
- </para>
- </sect3>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 et:
- -->
|