php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46143 Serializing SimpleXML removes the root node; asXML on unserailized breaks
Submitted: 2008-09-21 11:37 UTC Modified: 2008-10-16 18:09 UTC
From: firehed at gmail dot com Assigned:
Status: Not a bug Package: SimpleXML related
PHP Version: 5.2.6 OS: OS X 10.5
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: firehed at gmail dot com
New email:
PHP Version: OS:

 

 [2008-09-21 11:37 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-09-21 15:12 UTC] 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-10-16 18:09 UTC] rrichards@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
 [2011-04-29 13:50 UTC] MartinWillms2000 at aol dot com
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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 12:01:30 2024 UTC