Home arrow PHP arrow Page 5 - Serializing XML With PHP

No Attribution - 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

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: