ID: 46737 User updated by: sites at hubmed dot org -Summary: SimpleXML output using asXML() should default to UTF-8 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've changed the title, as I found that using DOM works the same way: === $dom = new DOMDocument(); $dom->loadXML('<?xml version="1.0" encoding="UTF-8"?><text>ü</text>'); print $dom->saveXML() . "\n"; // UTF-8 $dom->loadXML('<?xml version="1.0"?><text>ü</text>'); print $dom->saveXML() . "\n"; // not UTF-8 print $dom->saveXML($dom->firstChild) . "\n"; // UTF-8 === Maybe it is supposed to work this way, but it's unintuitive and it would be useful to know why. Previous Comments: ------------------------------------------------------------------------ [2008-12-03 14:31:15] sites at hubmed dot org I wouldn't call ü 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. ------------------------------------------------------------------------ [2008-12-03 14:29:13] [EMAIL PROTECTED] This <?xml version="1.0"?> <text>umlaut ü 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 ü here</text> ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=46737&edit=1