#46143 [Com]: Serializing SimpleXML removes the root node; asXML on unserailized breaks

From: Date: Sun, 21 Sep 2008 15:12:06 +0000
Subject: #46143 [Com]: Serializing SimpleXML removes the root node; asXML on unserailized breaks
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-129154@lists.php.net to get a copy of this message
ID: 46143 Comment by: phpwnd at gmail dot com Reported By: firehed at gmail dot com Status: Open Bug Type: SimpleXML related Operating System: OS X 10.5 PHP Version: 5.2.6 New Comment: AFAIK, you cannot serialize a SimpleXMLElement, because it's a resource wrapped inside an object and it would be impossible de restore that resource upon unserialize()-ation. That's why you receive an error: the unserialized object doesn't know where's the original document anymore. Store it in text form instead (asXML()). Proposed solution #1: prevent SimpleXMLElement serialization, or make it produce an E_NOTICE. The error message could advise the user to store SimpleXMLElements as XML strings instead. Proposed solution #2: allow serialization, make the serialized representation store the XML as a string and recreate the [copy of the original] document upon deserialization. Previous Comments: ------------------------------------------------------------------------ [2008-09-21 11:37:24] firehed at gmail dot com Description: ------------ If you serialize a SimpleXMLElement object, the root-level node is lost, causing the unserialize() event to issue a warning-level error (and creating an empty SimpleXMLElement object) and cause most other SimpleXMLElement methods to fail with a warning-level error. Reproduce code: --------------- <?php $xml = new SimpleXMLElement('<test><foo>1</foo><bar>2</bar></test>'); $serialized = serialize($xml); $unserialized = unserialize($serialized); $unserialized->addChild('testing', 'worked'); echo $unserialized->asXML(); Expected result: ---------------- <?xml version="1.0"?> <test><foo>1</foo><bar>2</bar><testing>worked</testing></test> Actual result: -------------- Warning: unserialize() [function.unserialize]: Node no longer exists in /Applications/MAMP/htdocs/serialxml.php on line 4 Warning: SimpleXMLElement::addChild() [function.SimpleXMLElement-addChild]: Node no longer exists in /Applications/MAMP/htdocs/serialxml.php on line 5 Warning: SimpleXMLElement::addChild() [function.SimpleXMLElement-addChild]: Cannot add child. Parent is not a permanent member of the XML tree in /Applications/MAMP/htdocs/serialxml.php on line 5 Warning: SimpleXMLElement::asXML() [function.SimpleXMLElement-asXML]: Node no longer exists in /Applications/MAMP/htdocs/serialxml.php on line 6 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46143&edit=1

« previous php.bugs (#129154) next »