PHP
  Home arrow PHP arrow Page 5 - 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 / 66
    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 - No Attribution


    (Page 5 of 10 )

    Now, what about those pesky attributes? Well, XML_Serializer comes with an option that allows you to represent array keys as attributes of the enclosing element (instead of elements themselves). Take a look:


    <?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");
     
    // represent scalar values as attributes instead of element
    $serializer->setOption("scalarAsAttributes", 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"? >
    <library>
        
    <book author="Charles Dickens" title="Oliver Twist" />
    </library>

    Note that in order for this to work, the array key which is to be represented as an attribute should point to a single scalar value and not another array or object. To understand this better, consider the following example, which demonstrates the difference:


    <?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",
        "price" => array ( "currency" =>
    "USD",
           "amount" =>
    24.50)));
     
    // add XML declaration
    $serializer->setOption("addDecl", true);
     
    // indent elements
    $serializer->setOption("indent", "    ");
     
    // set name for root element
    $serializer->setOption("rootName", "library");
     
    // represent scalar values as attributes instead of element
    $serializer->setOption("scalarAsAttributes", true);
          

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

    And here's the revised output:


    <?xml version="1.0"? >
    <library>
        
    <book author="Charles Dickens" title="Oliver Twist">
            
    <price amount="24.5" currency="USD" />
        
    </book>
    </library>

    To add attributes to the root node, set them with the rootAttributes option, as below:


    <?php
     
    // include class file
    include("Serializer.php");
     
    // create object
    $serializer = new XML_Serializer();
     
    // create array
    $xml = array("name" => "John Doe", "age" => 34, "sex" => "male");
     
    // add XML declaration
    $serializer->setOption("addDecl", true);
     
    // indent elements
    $serializer->setOption("indent", "    ");
     
    // set name for root element
    $serializer->setOption("rootName", "person");
     
    // set attributes for root element
    $serializer->setOption("rootAttributes", array("id" => 346747));
     
    // 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"? >
    <person id="346747">
        
    <name>John Doe</name>
        
    <age>34</age>
        
    <sex>male</sex>
    </person>

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


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - 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





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