#46737 [Bgs]: SimpleXML output using asXML() should default to UTF-8

From: Date: Wed, 03 Dec 2008 14:31:16 +0000
Subject: #46737 [Bgs]: SimpleXML output using asXML() should default to UTF-8
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-131564@lists.php.net to get a copy of this message
ID: 46737 User updated by: sites at hubmed dot org Reported By: sites at hubmed dot org Status: Bogus Bug Type: SimpleXML related Operating System: Mac OS X PHP Version: 5.2.6 New Comment: I wouldn't call &#xFC; UTF-8, I'd call it a numerical entity representation of a UTF-8 character, which is what libxml2 falls back on when the output contains a character that can't be handled by the output encoding. Previous Comments: ------------------------------------------------------------------------ [2008-12-03 14:29:13] chregu@php.net This <?xml version="1.0"?> <text>umlaut &#xFC; here</text> is UTF-8, too. Just written differently. Nothing wrong with the output (otherwise it would be invalid, as the default encoding is UTF-8, if there's nothing declared in the <?xml header. ------------------------------------------------------------------------ [2008-12-03 11:41:07] sites at hubmed dot org Description: ------------ Using $xml->asXML() to output an XML document as a string from SimpleXML seems to be defaulting to ISO 8859-1 rather than UTF-8, despite all other operations being in UTF-8 (and with LANG and LC_ALL being set to UTF-8). There is a workaround, by manually setting '<?xml version="1.0" encoding="UTF-8"?>' at the start of any imported XML, but it seems strange that there isn't anywhere to set this default permanently. The behaviour of asXML() also seems to vary when printing part of a SimpleXML object (where it uses UTF-8) rather than the whole document (where it uses ISO 8859-1). Adding putenv('LANG=en_GB.UTF-8'); setlocale(LC_ALL, 'en_GB.UTF-8'); to the script doesn't seem to help. Reproduce code: --------------- // manually set encoding to UTF-8 $doc = simplexml_load_string('<?xml version="1.0" encoding="UTF-8"?><text>umlaut ü here</text>'); print $doc->asXML() . "\n"; // defaults to UTF-8 $doc = simplexml_load_string('<doc><text>umlaut ü here</text></doc>'); print $doc->text->asXML() . "\n\n"; // defaults to ISO 8859-1 $doc = simplexml_load_string('<text>umlaut ü here</text>'); print $doc->asXML() . "\n"; Expected result: ---------------- <?xml version="1.0" encoding="UTF-8"?> <text>umlaut ü here</text> <text>umlaut ü here</text> <?xml version="1.0"?> <text>umlaut ü here</text> Actual result: -------------- <?xml version="1.0" encoding="UTF-8"?> <text>umlaut ü here</text> <text>umlaut ü here</text> <?xml version="1.0"?> <text>umlaut &#xFC; here</text> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46737&edit=1

« previous php.bugs (#131564) next »