php xml serialize and unserialize with pear

1.2k Views Asked by At

I have noticed PEAR XML_Serializer and XML_Unserializer are not "reversible", for example :

<?php
$a = array(1, 2);
$serializer = new XML_Serializer();
$serializer->serialize($a);
$b = $serializer->getSerializedData(); 
print($b); // "<array><XML_Serializer_Tag>1</Xml_Serializer_Tag><XML_Serializer_Tag>2</Xml_Serializer_Tag></array>"
$unserializer = new XML_Unserializer(); 
$unserializer->unserialize($b);
$c = $unserializer->getUnserializedData();
print_r($c); // array('XML_Serializer_Tag' => array(1, 2)) instead of array(1, 2)

Are there any options for XML_Unserializer to remove Xml_Serializer_Tag recursively ?

1

There are 1 best solutions below

1
On

I found my answer in XML_Unserializer options PEAR Documentation

I modified a line in my code above and it worked :

$unserializer = new XML_Unserializer(array(XML_UNSERIALIZER_OPTION_IGNORE_KEYS => array('XML_Serializer_Tag'))); 

Too bad this is not the default value...