2008/11/26

php serialize and serialize SimpleXMLElement

在PHP5中,序列化/反序列化simpleXMLElement对象时发生Warning: unserialize() [function.unserialize]: Node no longer exists in...的错误。

查了一下,http://bugs.php.net/bug.php?id=46143&edit=1,里面有段描述:

AFAIK, you cannot serialize a SimpleXMLElement, because it's a resource wrapped inside an object and it would be impossible de restore thatresource upon unserialize()-ation. That's why you receive an error: theunserialized object doesn't know where's the original document anymore.Store it in text form instead (asXML()).
说是SimpleXMLElement这个对象内部是包装的资源,即php中的resource类型,而resource是不可以序列化的。

上述文字提供了一个解决方案,保存其xml字符串,然后再通过simplexml_load_file/simplexml_load_string恢复SimpleXMLElement对象即可,xml字符串可以通过SimpleXMLElement->asXML()得到。

没有评论:

发表评论