php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61597 SimpleXMLElement doesn't include both @attributes and textContent in properties
Submitted: 2012-04-02 03:37 UTC Modified: 2021-03-12 07:48 UTC
Votes:16
Avg. Score:4.4 ± 0.7
Reproduced:16 of 16 (100.0%)
Same Version:5 (31.2%)
Same OS:4 (25.0%)
From: drgroove at gmail dot com Assigned:
Status: Re-Opened Package: SimpleXML related
PHP Version: 8.0.3 OS: Any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: drgroove at gmail dot com
New email:
PHP Version: OS:

 

 [2012-04-02 03:37 UTC] drgroove at gmail dot com
Description:
------------
When loading XML into SimpleXML, tags which have both attributes and text will have their attributes dropped.  

For example, loading this into SimpleXML: 
<sometag attribute="value">Here is some text</sometag>

Will cause 'attribute="value"' to be lost.  If there is no text w/in a tag, but attributes, the attributes and their values are preserved. 

I'm not the only PHP developer to notice this bug; please see: 
http://stackoverflow.com/questions/8563073/disappearing-attributes-in-php-simplexml-object

I didn't see this bug reported on php.net, however. 

Test script:
---------------
See above. 

Expected result:
----------------
Tags with both attributes and text should not have their attributes dropped. 


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-02 06:41 UTC] aharvey@php.net
I can't reproduce this:

<?php
$xml = <<<'EOX'
<?xml version="1.0"?>
<parent>
    <child attr="foo">text content</child>
</parent>
EOX;

$doc = simplexml_load_string($xml);
printf("Text content: %s; attribute: %s\n", $doc->child, $doc->child['attr']);
?>

Outputs "Text content: text content; attribute: foo", as I'd expect. Are you 
able to provide a reproduction script?

The SO discussion appears to be about JSON encoding only; given that 
SimpleXMLElement doesn't support serialisation, I'd suggest that the same would 
apply to JSON encoding.
 [2012-04-02 06:41 UTC] aharvey@php.net
-Status: Open +Status: Feedback
 [2012-04-12 19:51 UTC] jplyon dot csu at gmail dot com
another test:

$xml = <<<'EOX'
<?xml version="1.0"?>
<data>
<datum file-key="8708124062829849862">corn</datum>
</data>
EOX;

$doc = simplexml_load_string($xml);

echo $doc->asXML();

echo print_r($doc);

This is not a problem with loading for me - I am building my SimpleXML object 
dynamically.
The output from asXML() is correct.
The output from print_r() is not.
The output using attributes() (in a more complex example) is not correct, 
although this simple test does work.

I am experiencing this on various versions of PHP 5.3.X on Windows Vista and 
Ubuntu 11.10.

(ubuntu)$ php -v
PHP 5.3.2-1ubuntu4.9 with Suhosin-Patch (cli) (built: May  3 2011 00:45:52)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
    with Suhosin v0.9.29, Copyright (c) 2007, by SektionEins GmbH

(Windows)>php -v
PHP 5.3.8 (cli) (built: Sep 22 2011 20:51:51)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
 [2012-04-13 01:59 UTC] aharvey@php.net
Ah, I see what you're getting at now. Thanks. It appears to be an issue with the 
get_properties and get_debug_info handlers for SimpleXMLElement.

This has been reported and closed as a duplicate before, but I think that was 
mistaken, as we don't have an open bug for this at present that I can find.
 [2012-04-13 01:59 UTC] aharvey@php.net
-Summary: SimpleXML drops attributes for tags with both attributes and text +Summary: SimpleXMLElement doesn't include both @attributes and textContent in properties -Status: Feedback +Status: Open
 [2012-07-04 16:31 UTC] hugh at infomenta dot com
I am having exactly the same problem and it is not feasible to add an extra tag to all of my "text" entries.
 [2012-11-26 22:37 UTC] dbai at drivenbi dot com
Is there any update on this one? A 3rd party PHP library I'm using has a defect 
which, I think, is caused by this problem because the phenomenon is that attribute 
in the XML node is not loaded, and that XML node happens to have text content.
 [2015-04-28 17:06 UTC] tlesher at mtadistributors dot com
Just by way of a note, this problem is still occurring in PHP 5.5.24.
 [2015-09-09 01:32 UTC] cmb@php.net
> This has been reported and closed as a duplicate before, but I
> think that was mistaken, as we don't have an open bug for this
> at present that I can find.

There is bug #44973, which has been closed as not a bug.
 [2020-03-06 16:55 UTC] cmb@php.net
-Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2020-03-07 10:32 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #61597: SXE properties may lack attributes and content
On GitHub:  https://github.com/php/php-src/pull/5246
Patch:      https://github.com/php/php-src/pull/5246.patch
 [2020-03-07 10:34 UTC] cmb@php.net
To clarify: not only attributes may be swallowed in debug output,
but also elements of mixed content.
 [2020-03-12 09:58 UTC] cmb@php.net
Automatic comment on behalf of cmbecker69@gmx.de
Revision: http://git.php.net/?p=php-src.git;a=commit;h=7c081db885756d7b176a55b90b8746f664d1e042
Log: Fix #61597: SXE properties may lack attributes and content
 [2020-03-12 09:58 UTC] cmb@php.net
-Status: Verified +Status: Closed
 [2020-05-01 10:44 UTC] cmb@php.net
-Status: Closed +Status: Re-Opened -Assigned To: cmb +Assigned To:
 [2020-05-01 10:44 UTC] cmb@php.net
The bad "fix" has been reverted – re-opnening.
 [2021-03-12 07:48 UTC] requinix@php.net
-Operating System: Mac OS X +Operating System: Any -PHP Version: 5.3.10 +PHP Version: 8.0.3
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC