Home arrow PHP arrow Page 7 - Serializing XML With PHP

Not My Type - PHP

Simplify the task of creating XML documents with the XML_Serializer class, which lets you build nested XML documents from PHP data structures like arrays and objects (and vice versa). I'll show you how to build an XML document tree via the XML_Serializer class from PEAR, how to programmatically create an XML document from an array or an object, how to attach attributes to elements, and how to customize the behavior of the serializer. All this, and much, much more!

TABLE OF CONTENTS:
  1. Serializing XML With PHP
  2. A Twist In The Tale
  3. Anatomy Class
  4. Total Satisfaction
  5. No Attribution
  6. An Object Lesson
  7. Not My Type
  8. Travelling In Reverse
  9. Keeping It Simple
  10. Linking Out
By: Melonfire
Rating: starstarstarstarstar / 73
April 14, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: