Home arrow PHP arrow Page 4 - Serializing XML With PHP

Total Satisfaction - 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

Now, if you're a nitpicker, the output of the example on the previous page still won't satisfy you. Here's why:

  1. The serialized XML document does not contain the XML declaration at the top.

  2. The root element of the document is called <array>, whereas what you actually want is for it to be <library>.

  3. The XML document is not correctly indented.

In order to account for these requirements, XML_Serializer comes with a setOption() method, which allows you to customize the behaviour of the serializer to your needs. To illustrate, consider the following example, which solves the first problem noted above:


<?php
 
// include class file
include("Serializer.php");
 
// create object
$serializer = new XML_Serializer();
 
// create array to be serialized
$xml = array ( "book" => array (
    "title" => "Oliver Twist", 
    "author" => "Charles Dickens"));
 
// add XML declaration
$serializer->setOption("addDecl", true);
 
// perform serialization
$result = $serializer->serialize($xml);
 
// check result code and display XML if success
if($result === true) 
{
 echo $serializer->getSerializedData();
}
 
? >

Here's the output:


<?xml version="1.0"? >
<array>
<book>
<title>Oliver Twist</title>
<author>Charles Dickens</author>
</book>
</array>

Thus, the setOption() method takes two arguments -- a variable and its value -- and uses that information to tell the serializer how to return the XML document.

Next, how about fixing the root element and the indentation?


<?php
 
// include class file
include("Serializer.php");
 
// create object
$serializer = new XML_Serializer();
 
// create array to be serialized
$xml = array ( "book" => array (
    "title" => "Oliver Twist", 
    "author" => "Charles Dickens"));
 
// add XML declaration
$serializer->setOption("addDecl", true);
 
// indent elements
$serializer->setOption("indent", "    ");
 
// set name for root element
$serializer->setOption("rootName", "library");
      

// perform serialization
$result = $serializer->serialize($xml);
 
// check result code and display XML if success
if($result === true) 
{
 echo $serializer->getSerializedData();
}
 
? >

And here's the result:


<?xml version="1.0"? >
<library>
    
<book>
        
<title>Oliver Twist</title>
        
<author>Charles Dickens</author>
    
</book>
</library>

Pretty, isn't it?



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: