Edit report at https://bugs.php.net/bug.php?id=61597&edit=1
ID: 61597
Comment by: jplyon dot csu at gmail dot com
Reported by: drgroove at gmail dot com
Summary: SimpleXML drops attributes for tags with both
attributes and text
Status: Feedback
Type: Bug
Package: SimpleXML related
Operating System: Mac OS X
PHP Version: 5.3.10
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2012-04-02 06:41:26] [email protected]
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 03:37:26] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=61597&edit=1