|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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. PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 15:00:01 2025 UTC |
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.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