php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46672 Cloned SimpleXML Output Different
Submitted: 2008-11-25 23:35 UTC Modified: 2010-05-27 11:09 UTC
Votes:5
Avg. Score:4.2 ± 1.0
Reproduced:5 of 5 (100.0%)
Same Version:4 (80.0%)
Same OS:5 (100.0%)
From: rfludwick at gmail dot com Assigned: rrichards (profile)
Status: Wont fix Package: SimpleXML related
PHP Version: 5.*, 6CVS (2009-04-01) OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
3 + 34 = ?
Subscribe to this entry?

 
 [2008-11-25 23:35 UTC] rfludwick at gmail dot com
Description:
------------
It appears that cloning a SimpleXML object will not produce the same XML output on asXML() as the original object. The original will output the XML header and a line break after the XML while the cloned object is missing these.

Reproduce code:
---------------
<?php
$simplexml = new SimpleXMLElement("<root />");
$simplexml->path->to->node = "Zend";

$simplexml_2 = clone $simplexml;

echo "|{$simplexml->asXML()}|\n|{$simplexml_2->asXML()}|";

Expected result:
----------------
|<?xml version="1.0"?>
<root><path><to><node>Zend</node></to></path></root>
|
|<?xml version="1.0"?>
<root><path><to><node>Zend</node></to></path></root>
|

Actual result:
--------------
|<?xml version="1.0"?>
<root><path><to><node>Zend</node></to></path></root>
|
|<root><path><to><node>Zend</node></to></path></root>|

Patches

simplexml-fix-xml-header-for-clone.patch (last revision 2010-04-05 01:46 UTC by compuart dot php at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-30 14:49 UTC] rfludwick at gmail dot com
I got the same result when using the snapshot, as well as version 5.2.8.
 [2010-04-05 03:51 UTC] compuart dot php at gmail dot com
The issue appears to be the cloned node doesn't have a document pointer as its ultimate parent. Attached patch should fix that.
 [2010-05-27 08:10 UTC] mike@php.net
-Status: Verified +Status: Assigned -Assigned To: +Assigned To: rrichards
 [2010-05-27 08:10 UTC] mike@php.net
Rob, could you have a look at this tiny patch?
 [2010-05-27 11:09 UTC] rrichards@php.net
-Status: Assigned +Status: Wont fix
 [2010-05-27 11:09 UTC] rrichards@php.net
The asXML() method is special in regards to the document element as it treats it 
as a document node for simple trees. A clone of a document element is a regular 
element (a document can only have 1) and does not get special treatment.

Patch breaks in memory tree. Either manually add the XML declaration to the clone 
or import to dom and clone the document and work off the new document.
 [2010-10-28 02:30 UTC] thedailynathan at gmail dot com
Is there a reason this is marked as won't fix? I ran into this today, and it 
causes basic expected behavior such as:

$simplexml2 = clone $simplexml;
$simplexml->asXML() == $simplexml2->asXML();

to fail (returns false)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Dec 03 08:01:23 2020 UTC