Solved

Saving XML file w/ UTF-8

Posted on 2008-10-21
6
1,173 Views
Last Modified: 2008-10-21
I'm trying to save an xml file formatted in UTF-8 but it doesn't appear to be working. Here is a condensed version of my code:

$xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

mysql query goes here...

$xml .= "mysql xml output";

            $folder = $_SERVER['DOCUMENT_ROOT']."/path";
            
            if(!(is_dir($folder))) {
            mkdir($folder,0777);
            }


  $tempfile = $folder."/".date("Ymd").".xml";
  $fo = fopen($tempfile, 'w') or die("Cannot create XML file.");
  fwrite($fo,utf8_decode($xml));
  fclose($fo);

For some reason, I open the file and the UTF-8 characters are not recognized (seen as question marks). In the MySQL db, the collation is set to utf8_unicode_ci. Is there a way to set the file encoding when I am opening the file or writing to it?

0
Comment
Question by:sherlock5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 15

Expert Comment

by:MMDeveloper
ID: 22768492
why are you decoding UTF8 before writing to the file? Wouldn't you need to utf8_encode when saving?
0
 

Author Comment

by:sherlock5
ID: 22768555
i also tried utf8_encode... utf8_decode was the only option that limited the coding problem to just a question mark. I also switched the  encoding="iso-8859-1" without luck.
0
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 22768568
You seem to be decoding the utf8 before you write it to the file:

fwrite($fo,utf8_decode($xml));

Then you save it as latin1. It should be just:

fwrite($fo,$xml);

...or, if the $xml is latin1 and the file should be utf8:

fwrite($fo,utf8_encode($xml));
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

Author Comment

by:sherlock5
ID: 22768818
Yeah, cxr, I have tried all of those options as well. When I try to use the function, header("Content-type: application/octet-stream; charset=UTF-8");, to output the file in a new window, the content is formatted in UTF-8... so how do I save the file as a UTF-8 formatted file?
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 250 total points
ID: 22769115
You say you open it and view it and see question marks, in what environment are you opening it? Through the web browser? If you view it in the browser and change character encoding in the browser, maybe you can identify what character encoding is in the file.

Your example says: $xml .= "mysql xml output";

I suppose the real $xml variable contains content grabbed directly from the database? Or is there any conversion involved? Is the character set used in the database utf8 (not just collation)? Execute this sql to find out:

show variables like "character_set%";
0
 

Author Comment

by:sherlock5
ID: 22769538
So, you were right cxr, I changed character_set_database to utf8... then i removed the utf8_encode... and then i opened the file in firefox and notepad, saw that the characters were showing up correctly and that the encoding was utf8. thanks for the help!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question