Edit report at http://bugs.php.net/bug.php?id=46143&edit=1
ID: 46143 Comment by: MartinWillms2000 at aol dot com Reported by: firehed at gmail dot com Summary: Serializing SimpleXML removes the root node; asXML on unserailized breaks Status: Bogus Type: Bug Package: SimpleXML related Operating System: OS X 10.5 PHP Version: 5.2.6 Block user comment: N Private report: N New Comment: I have the same problem when using simplexml and reading a string entry from a node. The node contents is the following: /mtdata/usb/data I use the following code to read the node contents into a variable: $temp_str = $xmlJobFile_cl->recording->target_directory; Using this code the problem described above occurs. Therefore I think that the simplexml node is serialised to $temp_str. Applying a type cast to string solved the problem: $temp_str = (string) $xmlJobFile_cl->recording->target_directory; >php-config --version 5.2.6 >apache2ctl -v Server version: Apache/2.2.3 Server built: Sep 25 2008 11:25:06 Previous Comments: ------------------------------------------------------------------------ [2008-10-16 18:09:56] rricha...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php For reasons as stated by phpwnd ------------------------------------------------------------------------ [2008-09-21 15:12:06] phpwnd at gmail dot com 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. ------------------------------------------------------------------------ [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/bug.php?id=46143&edit=1