php XML DOM translates special chars to &#xYY;

邮差的信 提交于 2019-12-04 12:11:11

The easiest way to fix this is to set the encoding type after you have loaded the XML:

$dom = new DOMDocument();
$dom->loadXML($data);
$dom->encoding = 'utf-8';

echo $dom->saveXML();                       
exit();

You can also fix it by putting an XML declaration at the beginning of your data:

$data = '<?xml version="1.0" encoding="utf-8"?>' . $data;
$dom = new DOMDocument();
$dom->loadXML($data);

echo $dom->saveXML();
exit();

I solved with this:

        header('Content-type: text/html; charset=utf-8');       
    if(isset($_POST) && isset($_POST['data']))
    {           
        $data = '<?xml version="1.0" encoding="utf-8"?>';
        $data .= '<ul id="zone_container" class="ui-sortable">';
        $data .= $_POST['data'];
        $data .= '</ul>';                   

        $dom = new DOMDocument('1.0', 'utf-8');
        $dom->loadXML($data);

        echo $dom->saveXML();                       
        exit();

adding the:

            $data = '<?xml version="1.0" encoding="utf-8"?>';

to the XML at the beginning

thanks for responses :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!