Data Exchange with XML, WDDX and PHP - Humbert Redfinch-Northbottom The Third, I Presume? (
Page 4 of 9 )
The process of converting a data structure into WDDX is referred to as "serialization". Deserialization is, obviously, the reverse process. And your favourite language and mine, PHP, comes with a number of WDDX functions to accomplish both these (although you may need to recompile your PHP build to enable them.)
The first of these is the wddx_serialize_value() function, used to create a WDDX packet containing a single value. The function also accepts an optional second parameter, which is used to generate a comment for the packet. So the following line of code
<?
echo wddx_serialize_value("Humbert Redfinch-Northbottom The Third", "Who da
man?");
?>
would generate the WDDX fragment
<wddxPacket version='1.0'>
<header>
<comment>Who da man?</comment>
</header>
<data>
<string>Humbert Redfinch-Northbottom The Third</string>
</data>
</wddxPacket>
Once a value has been serialized, it can be reconstituted into its original form with the wddx_deserialize() function, used to convert a WDDX packet into a native data structure. Consider the following example,
<?
// serialize
$packet = wddx_serialize_value("Humbert Redfinch-Northbottom The Third");
// deserialize
$original_string = wddx_deserialize($packet);
// print
echo "My name is $original_string";
?>
which generates the output
My name is Humbert Redfinch-Northbottom The Third
This also works with arrays - the following code
<?
$star_wars = array("princess" => "Leia", "teacher" => "Yoda", "new hope" =>
"Luke", "bad guy" => "Darth", "worse guy" => "The Emperor");
$packet = wddx_serialize_value($star_wars);
$data = wddx_deserialize($packet);
echo $data['princess'];
?>
outputs
Leia