2

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......

2
  • Try using utf8_encode() (php.net/manual/en/function.utf8-encode.php) on the text string before you insert it into the DOMDocument object Commented Apr 3, 2012 at 17:36
  • The interesting is that if the target document has the starting <?xml version="1.0" encoding="utf-8"?> Then the final output works perfact. Commented Apr 4, 2012 at 3:59

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.