Home arrow PHP arrow Page 2 - Serializing XML With PHP

A Twist In The Tale - 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

The XML_Serializer class comes courtesy of PEAR, the PHP Extension and Application Repository (http://pear.php.net), and has been developed by Stephan Schmidt of phptools.de fame. In case you didn't know, PEAR is an online repository of free PHP software, including classes and modules for everything from data archiving to XML parsing. When you install PHP, a whole bunch of PEAR modules get installed as well.

In case your PHP distribution didn't include XML_Serializer, you can get yourself a copy from the official PEAR Web site, at http://pear.php.net - simply unzip the distribution archive into your PEAR directory and you're ready to roll!

Note that in order to use XML_Serializer, you will need to have the XML_Util package already installed. If you don't already have it, you can get it from the Web site above.

Let's begin with something simple: dynamically constructing an XML document from a PHP array. Here's the code:


<?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"));
 
// perform serialization
$result = $serializer->serialize($xml);
 
// check result code and display XML if success
if($result === true) 
{
 echo $serializer->getSerializedData();
}
 
? >

Don't worry if it didn't make too much sense; all will be explained shortly. For the moment, just feast your eyes on the output (note that you may need to use the "View Source" feature of your browser to see this):

<array>
<book>
<title>Oliver Twist</title>
<author>Charles Dickens</author>
</book>
</array>

As you can see, the output of the script is a well-formed XML document -- all created using PHP code!



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