?
Solved

XML Parsing error with accent character

Posted on 2009-12-25
6
Medium Priority
?
515 Views
Last Modified: 2012-05-08
I am getting an XML parsing error whenever my code runs into an accent character such as:

áéíóúâä

Is there some way I can alter my code to handle for these and all other special characters?


<?php 

header("Content-type: text/xml"); 

$host = "localhost"; 
$user = "root"; 
$pass = "root"; 
$database = "mydatabase"; 

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); 
mysql_select_db($database, $linkID) or die("Could not find database."); 

$query = "SELECT * FROM tblclients"; 

$resultID = mysql_query($query, $linkID) or die("Data not found."); 

$xml_output = "<?xml version=\"1.0\"?>\n"; 
echo $xml_output."<optc>\n"; 
while ($line = mysql_fetch_assoc($resultID) ) {
  echo "<record>\n";
  foreach($line as $key=>$value)
  	  echo "  <$key>". htmlentities($value) . "</$key>\n";	
  echo "</record>\n";
}
echo '</optc>'; 
?>

Open in new window

0
Comment
Question by:DanielAttard
  • 3
  • 3
6 Comments
 

Author Comment

by:DanielAttard
ID: 26123451
What is I also need to handle for a carriage return being entered within the data?  I get a parsing error with any data that has a carriage return within the field data.  Thanks.
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26123454
Hello DanielAttard,

No $key with accent too ?
Try for example :


....
while ($line = mysql_fetch_assoc($resultID) ) {
  echo "<record>\n";
  foreach($line as $key=>$value)
          $k = strtr($key,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ', 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
          echo "  <$k>". htmlentities($value) . "</$k>\n";  
  echo "</record>\n";
}
....

Open in new window

0
 
LVL 83

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 26123469
To remove carriage return, linefeed and tabulation : preg_replace("/[\n|\r|\t]/", "", $value)


....
while ($line = mysql_fetch_assoc($resultID) ) {
  echo "<record>\n";
  foreach($line as $key=>$value)
          $k = strtr($key,'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ', 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY');
          echo "  <$k>". htmlentities(preg_replace("/[\n|\r|\t]/", "", $value)) . "</$k>\n";  
  echo "</record>\n";
}
....

Open in new window

0
Industry Leaders: 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!

 

Author Closing Comment

by:DanielAttard
ID: 31669996
Thanks leakim.  
0
 
LVL 83

Expert Comment

by:leakim971
ID: 26123518
You're welcome! Merry Christmas!
0
 

Author Comment

by:DanielAttard
ID: 26123526
Merry Christmas to you too!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 5 hours left to enroll

864 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