PHP : Objects and XML

Just some nice example to show how to create an XML output from an object. And also create an object from an XML source.

Do be able to run those example, you must install the PEAR packages called XML/Serializer.

First example, creation of the XML output:

Just some nice example to show how to create an XML output from an object. And also create an object from an XML source.

Do be able to run those example, you must install the PEAR packages called XML/Serializer.

First example, creation of the XML output:

<?php
require_once 'XML/Serializer.php';
// object definition
class Automobile
{
 // object properties
 public $color;
 public $year;
 public $model;
 function setAttributes($c, $y, $m)
 {
   $this->color = $c;
   $this->year = $y;
   $this->model = $m;
 }
 function xmlSerializeMe()
 {
   $serializer = new XML_Serializer();
   // add XML declaration
   $serializer->setOption("addDecl", true);
   // indent elements
   $serializer->setOption("indent", "   ");
   // set name for root element
   $serializer->setOption("rootName", get_class($this));
   // perform serialization
   $serializer->serialize($this);
   return $serializer;
 }
}
// create object to be serialized
$car = new Automobile();
$car->setAttributes("blue", 1982, "Mustang");
$result=$car->xmlSerializeMe();
echo $result->getSerializedData();
?>

Result :

<?xml version="1.0"?>
<Automobile>
   <color>blue</color>
   <year>1982</year>
   <model>Mustang</model>
</Automobile>

Second example, creation of an object from the XML source:

<?php
require_once 'XML/Serializer.php';
require_once 'XML/Unserializer.php';
$xml_doc = <<<EOF
<?xml version="1.0"?>
<Automobile>
   <color>blue</color>
   <year>1982</year>
   <model>Mustang</model>
</Automobile>
EOF;
// object definition
class Automobile
{
 // object properties
 public $color;
 public $year;
 public $model;
 function setAttributes($c, $y, $m)
 {
   $this->color = $c;
   $this->year = $y;
   $this->model = $m;
 }
 function xmlSerializeMe()
 {
   $serializer = new XML_Serializer();
   // add XML declaration
   $serializer->setOption("addDecl", true);
   // indent elements
   $serializer->setOption("indent", "   ");
   // set name for root element
   $serializer->setOption("rootName", get_class($this));
   // perform serialization
   $serializer->serialize($this);
   return $serializer;
 }
}
$unserializer = new XML_Unserializer();
$unserializer->setOption('complexType', 'object');
$unserializer->unserialize($xml_doc);
$car = $unserializer->getUnserializedData();
echo "<pre>";
print_r($car);
echo "</pre>";
?>

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Leave a Reply

Your email address will not be published. Required fields are marked *

As MySQL Community Manager, I am an employee of Oracle and the views expressed on this blog are my own and do not necessarily reflect the views of Oracle.

You can find articles I wrote on Oracle’s blog.