2

I have a XML file which have accented characters like æøåêèé. If I simply read the file using fread I can read these characters easily but if I use simplexml_load_string or DOMDocument I am not able to read these characters.

Same is the case with my JSON file where I tried using json_decode

I have tried using mb_convert_encoding and changing changing characters to Window-1252,UTF-8 and many other encoding but nothing work. I am using PHP 5.3.1. I would like somebody to help with a demo code of reading such charcaters. Following is my basic code which I tried but it didn't worked

XML File

    <?xml version="1.0" encoding="windows-1252"?>
    <note>
    <message>Norwegian: æøå. French: êèé</message>
    </note>

PHP Code

   $myFile = "check.xml";
   $fh = fopen($myFile, 'r');
   $theData = fread($fh, filesize($myFile));
   fclose($fh);
   echo $theData."<br>";
   $xml = simplexml_load_string($theData);
   print_r(mb_convert_encoding($xml->message,'Windows-1252'));
Gunjan Nigam
  • 1,363
  • 4
  • 10
  • 18

1 Answers1

1

try this

$fc = iconv('windows-1250', 'utf-8', file_get_contents(check.xml));
$handle=fopen("abc.xml", "rw");
fwrite($handle, $fc);
fclose($handle);

try working with this file which written using above method

maxjackie
  • 22,386
  • 6
  • 29
  • 37
  • The file written using this is not correct. First of all and most importantly it destroys the accented characters Norwegian: ćřĺ. French: Ä™Äé Secondly, the encoding is incorrect. The original XML encoding is windows-1252 so I changed the encoding in above method but still the accnted characters are destroyed – Gunjan Nigam Feb 15 '12 at 07:11
  • @GunjanNigam: are you absolutely sure the "check.xml" file is written with the "windows-1252" encoding? Otherwise it won't work (or you have to change the `encoding="windows-1252"`-part of your xml to the actual encoding of the file). – vstm Feb 15 '12 at 07:21