php XML DOM translates special chars to &#xYY;

匆匆过客 提交于 2020-01-01 16:24:09

问题


I send this with AJAX POST:

<li><ul class   "zone zCentral ui-sortable"><li><ul class="region rCol3 ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span> <span>video: 'Mundo.Hoy': ¿Dónde habré olvidado... mi memoria?</span></div></li></ul></li></ul></li>

I do this to create XML:

        header('Content-type: text/html; charset=utf-8');       
    if(isset($_POST) && isset($_POST['data']))
    {           
        $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();
    }

and i get this:

<?xml version="1.0"?>
<ul id="zone_container" class="ui-sortable">
    <li><ul class="zone zCentral ui-sortable"><li><ul class="region rCol3         ui-sortable"><li class="" style=""><div><span class="tc tc_video">574081</span>     <span>video: 'Mundo.Hoy': &#xBF;D&#xF3;nde habr&#xE9; olvidado... mi memoria?</span></div>    </li></ul></li></ul></li></ul>

¿Dónde habré olvidado... mi memoria?

translates to:

&#xBF;D&#xF3;nde habr&#xE9 ; olvidado... mi memoria?

I need original chars in the XML, these are utf-8 valid and i don't know the reason for this encode :(


回答1:


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();



回答2:


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 :)



来源:https://stackoverflow.com/questions/4625173/php-xml-dom-translates-special-chars-to-xyy

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