php XML DOM translates special chars to &#xYY;
Asked Answered
P

2

3

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

Prunella answered 7/1, 2011 at 11:47 Comment(2)
DOM will disregard how you instantiated it when loading markup and fall back to what is given in the markup.Irenics
possible duplicate of php: using DomDocument whenever I try to write UTF-8 it writes the hexadecimal notation of it.Irenics
P
5

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();
Pelargonium answered 7/1, 2011 at 12:0 Comment(0)
P
0

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

Prunella answered 7/1, 2011 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.