PHP
  Home arrow PHP arrow Page 4 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 / 65
    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 - Total Satisfaction


    (Page 4 of 10 )

    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


     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway