Home arrow PHP arrow Page 6 - Serializing XML With PHP

An Object Lesson - PHP

Simplify the task of creating XML documents with the XML_Serializer class, which lets you build nested XML documents from PHP data structures like arrays and objects (and vice versa). I'll show you how to build an XML document tree via the XML_Serializer class from PEAR, how to programmatically create an XML document from an array or an object, how to attach attributes to elements, and how to customize the behavior of the serializer. All this, and much, much more!

  1. Serializing XML With PHP
  2. A Twist In The Tale
  3. Anatomy Class
  4. Total Satisfaction
  5. No Attribution
  6. An Object Lesson
  7. Not My Type
  8. Travelling In Reverse
  9. Keeping It Simple
  10. Linking Out
By: Melonfire
Rating: starstarstarstarstar / 73
April 14, 2004

print this article



You can also serialize objects, in much the same way as you serialize arrays. Take a look at the following example, which demonstrates how:

// object definition
class Automobile 
 // object properties
 var $color;
 var $year;
 var $model;
function setAttributes($c$y$m)
->color $c;
->year $y;
->model $m;

// include class file
// create object
$serializer = new XML_Serializer();
// create object to be serialized
$car = new Automobile;
$car->setAttributes("blue", 1982, "Mustang");
// add XML declaration
$serializer->setOption("addDecl", true);
// indent elements
$serializer->setOption("indent", "    ");
// set name for root element
$serializer->setOption("rootName", "car");
// perform serialization
$result = $serializer->serialize($car);
// check result code and display XML if success
if($result === true) 
 echo $serializer->getSerializedData();
? >

In this example, I've first defined a class called Automobile, and created some methods and properties for it. Then, further down in the script, I've instantiated an object of the class and set some very specific values for the object's properties. This object has then been serialized via XML_Serializer's serialize() method.

Here's the result:

<?xml version="1.0"? >

>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: