#46143 [Opn->Bgs]: Serializing SimpleXML removes the root node; asXML on unserailized breaks
| From: | rrichards@php.net | Date: | Thu, 16 Oct 2008 18:09:56 +0000 |
| Subject: | #46143 [Opn->Bgs]: Serializing SimpleXML removes the root node; asXML on unserailized breaks | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-129761@lists.php.net to get a copy of this message | ||
ID: 46143
Updated by: rrichards@php.net
Reported By: firehed at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: SimpleXML related
Operating System: OS X 10.5
PHP Version: 5.2.6
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[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/?id=46143&edit=1