PHP
  Home arrow PHP arrow Page 7 - 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 - Not My Type


    (Page 7 of 10 )

    One of XML_Serializer's other interesting features is its ability to store data type information along with each value in the XML document. Called typeHints, this data type information can help in distinguishing between the integer 6 and the string "6", and comes in handy if your XML application is strongly typed.

    To enable type hints, you need to simply set the typeHints option to true. The following example illustrates:


    <?php
     
    // include class file
    include("Serializer.php");
     
    // set options
    $options = array( "addDecl" => true,
       "indent" => "    ",
       "rootName" => "car",
       "typeHints" => true);
     
    // create object
    $serializer = new XML_Serializer($options);
     
    // create array
    $car = array("color" => "blue", "year" => 1982, "model" => "Mustang",
    "price" => 15000.00);
     
    // perform serialization
    $result = $serializer->serialize($car);
     
    // check result code and display XML if success
    if($result === true) 
    {
     echo $serializer->getSerializedData();
    }
     
    ? >

    Once type hints are enabled, every element within the XML document will bear an additional attribute indicating the data type of the value contained within it. Here's what the output of the example above looks like:


    <?xml version="1.0"? >
    <car _type="array">
        
    <color _type="string">blue</color>
        
    <year _type="integer">1982</year>
        
    <model _type="string">Mustang</model>
        
    <price _type="double">15000</price>
    </car>

    Note that in the example above, I've used a slightly different method to set serializer options -- I've created an array of options and values, and passed the array to the object constructor. When you have a large number of options to set, this method can save you a few lines of code.

    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 5 hosted by Hostway