Solved

Saving XML file w/ UTF-8

Posted on 2008-10-21
6
1,170 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

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