?
Solved

Saving XML file w/ UTF-8

Posted on 2008-10-21
6
Medium Priority
?
1,187 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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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