php xml用pear序列化和反序列化

时间:2013-12-03 11:53:49

标签: php xml serialization xml-serialization pear

我注意到PEAR XML_SerializerXML_Unserializer不是“可逆的”,例如:

<?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)

XML_Unserializer是否有任何选项可以递归删除Xml_Serializer_Tag

1 个答案:

答案 0 :(得分:0)

我在XML_Unserializer options PEAR Documentation

中找到了答案

我在上面的代码中修改了一行并且它有效:

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

太糟糕了,这不是默认值......