Shahid
Shahid

Reputation: 1071

PHP, XML, DOM --- how to make sure that final xml file encoding=utf-8?

Please help me...

here is the detail scenario..

I have an xml file containing xml tags e.g.

data.xml - following its content
-----------------
<data>
<node1>some text</node1>
</data>
-------------------

Now I uploaded this file to my translations service. my code loads the file... following is the php code

$dom = new DOMDocument('1.0', 'utf-8');
if ( !$dom->load($target_file) ) {
   echo "Cannot load file $target_file";  
   exit;
}

then my logic operates and replaces the node value with some accented characters e.g. nënë and it works fine and finally i save the file

$dom->save($target_file);

Now the output should be like as follow

data.xml - following its content
-----------------
<?xml version="1.0" encoding="utf-8"?>
<data>
<node1>nënë</node1>
</data>
-------------------

BUT When i open the file the output as follow

-------------------
<?xml version="1.0"?>
<data>
<node1>n&#xEB;n&#xEB;</node1>
</data>
-------------------

please Help me ... How should I make sure that xml file encoding should be UTF-8?????

Waiting......

Upvotes: 1

Views: 1283

Answers (1)

Tom Imrei
Tom Imrei

Reputation: 1544

Don't know if you already solved it or not:

If your data is UTF-8-encoded and you discover that saveXML() turned all non-ASCII characters into numeric entities (e.g. ä -> &#xF6;):

Chances are that the XML declaration has been missing when you loaded the source data. Try adding <?xml version="1.0" encoding="UTF-8"?> to the beginning of the document before you read it with load() or loadXML(). Then the non-ASCII characters should remain untouched. Worked for me.

Source: http://www.php.net/manual/en/domdocument.savexml.php#97434

Upvotes: 1

Related Questions