PHP
  Home arrow PHP arrow Page 8 - 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 - Travelling In Reverse
    ( Page 8 of 10 )

    Good things come in twos: Mickey and Donald, Tom and Jerry, yin and yang. It's no surprise then that XML_Serializer has a doppelganger of its own. Called XML_Unserializer, this class can take an XML document and convert it into a series of nested PHP structures, suitable for use in a PHP script.

    In order to understand how this works, consider the following XML document:


    <?xml version='1.0'? >
    <library>
     
    <book id="MFRE001">
      
    <title>The Adventures of Sherlock Holmes</title>
      
    <author>Arthur Conan Doyle</author>
      
    <price currency="USD">24.95</price>
     
    </book>
     
    <book id="MFRE002">
      
    <title>Life of Pi</title>
      
    <author>Yann Martel</author>
      
    <price currency="USD">7.99</price>
     
    </book>
     
    <book id="MFRE003">
      
    <title>Europe on a Shoestring</title>
      
    <author>Lonely Planet</author>
      
    <price currency="USD">16.99</price>
     
    </book>
    </library>

    Now, in order to convert this XML document into a PHP structure, simply put XML_Unserializer to work on it, as below:


    <?php
     
    // include class file
    include("Unserializer.php");
     
    // create object
    $unserializer = &new XML_Unserializer();
     
    // unserialize the document
    $result = $unserializer->unserialize("library.xml", true);    
     
    // dump the result
    $data = $unserializer->getUnserializedData();
    print_r($data);
     
    ? >

    Here, the unserialize() method accepts either a string containing XML data or an XML file (set the second argument to false or true depending on which one you are passing) and returns a PHP structure representing the XML document. Here's what the output looks like:


    Array
    (
        
    [book] => Array
            
    (
                
    [0] => Array
                    
    (
                        
    [title] => The Adventures of Sherlock Holmes
                        
    [author] => Arthur Conan Doyle
                        
    [price] => 24.95
                    
    )
     
                [
    1] => Array
                    
    (
                        
    [title] => Life of Pi
                        
    [author] => Yann Martel
                        
    [price] => 7.99
                    
    )
     
                [
    2] => Array
                    
    (
                        
    [title] => Europe on a Shoestring
                        
    [author] => Lonely Planet
                        
    [price] => 16.99
                    
    )
            )
    )


    Now, in order to access the title of the third book (for example), you would use the notation


    $data['book'][2]['title'];

    which would return

    Europe on a Shoestring

    Note that XML_Unserializer uses the type hints generated in the serialization process to accurately map XML elements to PHP data types. If these hints are unavailable (as in the example above), XML_Unserializer will "guess" the type of each value. A look at the source code of the class reveals that "complex structures will be arrays and tags with only CData in them will be strings."



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT