previousXML_Serializer (Previous) (Next) XML_Serializer パッケージの定数next

View this page in Last updated: Sun, 21 Jun 2009
English | Brazilian Portuguese | Chinese | Dutch | French | German | Hungarian | Japanese | Polish | Russian | Spanish | Turkish

導入

導入 – XML_Serializer 導入

XML_Serializer 導入

XML_Serializer は配列やオブジェクトのような複雑なデータ構造を XML ドキュメントにシリアライズします。 このクラスは、DOM の必要なしに、あなたが必要とする XML ドキュメントを生成する手助けをします。

現時点で、XML_Serializer をアプリケーションで使用する 2つの方法があります:

  • 特定の XML アプリケーション (たとえば RDF) で XML 文章を生成するために XML_Serializer を関数的に使用する

  • あとでアンシリアライズすべく、 データ構造をシリアライズするために XML_Serializer を関数的に使用する。 これは、全ての XML 要素に型情報を与えることで可能となる。

このパッケージは、シリアライザクラスだけではなく、適応する XML_Unserializer も含んでいます。 これは、どのような XML ドキュメントも仮想的に読み込むことができ、 文章にストアされたデータを表す配列かオブジェクトを返します。

XML_Serializer のチュートリアル

XML_Serializer についてのいくつかのチュートリアルが利用可能です。 これは、あなたが使い始めることの手助けをします。

previousXML_Serializer (Previous) (Next) XML_Serializer パッケージの定数next

Download Documentation Last updated: Sun, 21 Jun 2009
Do you think that something on this page is wrong? Please file a bug report or add a note.
User Notes:
Note by: pear@eric.fambus.nl
To circumvent this problem, you might add the 'mode' => 'simplexml' option to the serializer object. This way, you can use structures like:

'test' => array
(
[0] => array
(
'title' => 'Item 1',
),
[1] => array
(
'title' => 'Item 2',
)
)

This will result in something like

<item>
<title>Item 1</title>
</item>
<item>
<title>Item 2</title>
</item>
Note by: mark_nr@earthlink.net
I spent a lot of time using this package, but it seems that in the end, it inherits limits imposed by arrays. Specifically, I couldn't figure out how to arrange for nested arrays.

For example. I'd like to create the following XML...

<root>
<contact>
<name>Mark</name>
<phoneNumbers>
<dial>5037771001</dial>
<dial>2129711001</dial>
<dial>4455543334</dial>
</phoneNumbers>
</contact>
<contact>
<name>Bob</name>
<phoneNumbers>
<dial>4334553445</dial>
<dial>1001179212</dial>
<dial>6124456669</dial>
</phoneNumbers>
</contact>
</root>

Because an associative array won't allow me to create two ore more elements with the same key, and because (it appears) that I can only have one default element name, I have to choose between multiple <contact>s or multiple <dial> - but not both.

I solved the problem by using the XML functions built into PHP 5 - specifically the SimpleXML class, which doesn't require creating an array, and allows elements to be added line by line.