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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 72
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT