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>