Introduction

Introduction – Introduction to XML_Serializer

Introduction to XML_Serializer

XML_Serializer serializes complex data structures like arrays or object as XML documents. This class helps you generating any XML document you require without the need for DOM.

Currently there are two ways in which XML_Serializer can be used in your applications:

  • use XML_Serializer's functionality to create XML documents in a specific XML application (e.g. RDF) that is being processed by an existing script)

  • use XML_Serializer's functionality to serialize data structures that have to be unserialized at a later point. This is possible by adding type information to all XML elements.

The package not only contains a serializer class but also a matching XML_Unserializer, which is able to virtually read any XML document and return an array or object structure that represents the data stored in the document.

Tutorials on XML_Serializer

There are several tutorials on XML_Serializer available, that help you get started.

XML_Serializer (Previous) Constants defined in and used by XML_Serializer (Next)
Last updated: Sat, 16 Feb 2019 — Download Documentation
Do you think that something on this page is wrong? Please file a bug report.
View this page in:
  • English

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.