PHP
  Home arrow PHP arrow Page 3 - Serializing XML With PHP
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Serializing XML With PHP
By: Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 68
    2004-04-14

    Table of Contents:
  • Serializing XML With PHP
  • A Twist In The Tale
  • Anatomy Class
  • Total Satisfaction
  • No Attribution
  • An Object Lesson
  • Not My Type
  • Travelling In Reverse
  • Keeping It Simple
  • Linking Out

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Serializing XML With PHP - Anatomy Class


    (Page 3 of 10 )

    Let's take a closer look at how I accomplished this.

    1. The first step is, obviously, to include the XML_Serializer class file:


    <?php
     
    // include class file
    include("Serializer.php");
     
    ? >

    You can either provide an absolute path to this file, or do what most lazy programmers do -- include the path to your PEAR installation in PHP's include_path variable, so that you can access any of the PEAR classes without needing to type in long, convoluted file paths.

    2. Next, an object of the class needs to be initialized, and assigned to a PHP variable.


    <?php
     
    // create object
    $serializer = new XML_Serializer();
     
    ? >

    This variable serves as the control point for future manipulation of XML_Serializer properties and methods.

    3. Next, you need to put together the data that you plan to encode in XML. The simplest way to do this is to create a nested set of arrays whose structure mimics that of the final XML document you desire.


    <?php
     
    // create array to be serialized
    $xml = array ( "book" => array (
        "title" => "Oliver Twist", 
        "author" => "Charles Dickens"));
     
    ? >

    4. With all the pieces in place, all that's left is to perform the transformation. This is done via the object's serialize() method, which accepts a PHP structure and returns a result code indicating whether or not the serialization was successful.


    <?php
     
    // perform serialization
    $result = $serializer->serialize($xml);
     
    ? >

    5. Once the serialization is complete, you can do something useful with it: write it to a file, pass it through a SAX parser or -- as I've done here -- simply output it to the screen for all to admire:


    <?php
     
    // check result code and display XML if success
    if($result === true) 
    {
     echo $serializer->getSerializedData();
    }
     
    ? >

    The getSerializedData() method returns the serialized XML document tree as is, and serves a very useful purpose in debugging. (You'll see it often over the next few pages.)

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


     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT