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: 2015-09-09 01:32 UTC
Votes:12
Avg. Score:4.4 ± 0.6
Reproduced:12 of 12 (100.0%)
Same Version:5 (41.7%)
Same OS:2 (16.7%)
From: drgroove at gmail dot com Assigned:
Status: Open Package: SimpleXML related
PHP Version: 5.3.10 OS: Mac OS X
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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

Add a Patch

Pull Requests

Add a Pull Request

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.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 11:01:36 2019 UTC