I have tried to unserialize a PHP Object.

Warning: unserialize() [function.unserialize]: Node no longer exists in /var/www/app.php on line 42

But why was that happen?

Even if I found a solution to unserialize simplexml objects, its good to know why php cant unserialize objects?

To serialize simplexml object i use this function

function serializeSimpleXML(SimpleXMLElement $xmlObj) 

        return serialize($xmlObj->asXML());


To unserialize an simplexml objetc i use this function

function unserializeSimpleXML($str) 

        return simplexml_load_string(unserialize($str));


Accepted Answer

SimpleXMLElement wraps a libxml resource type. Resources cannot be serialized. On the next invocation, the resource representing the libxml Node object doesn't exist, so unserialization fails. It may be a bug that you are allowed to serialize a SimpleXMLElement at all.

Your solution is the correct one, since text/xml is the correct serialization format for anything XML. However, since it is just a string, there isn't really any reason to serialize the XML string itself.

Note that this has nothing inherently to do with "built-in" PHP classes/objects, but is an implementation detail of SimpleXML (and I think DOM in PHP 5).

Written by Mike
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki