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!

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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


<?php
 
// object definition
class Automobile 
{
 
 // object properties
 var $color;
 var $year;
 var $model;
 
 
function setAttributes($c$y$m)
 
{
  $this
->color $c;
  $this
->year $y;
  $this
->model $m;
 
}
}

// include class file
include("Serializer.php");
 
// 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"? >
<car>
    
<color>blue</color>
    
<year>1982</year>
    
<model>Mustang</model>
</car>



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 1 - Follow our Sitemap

Dev Shed Tutorial Topics: