Charset in XML output from SQL .. german characters .. where am I going wrong?

微笑、不失礼 提交于 2020-01-06 19:53:15

问题


Thanks for taking the time!

I am trying to put together a mapping app to experiment with the Google Maps V3 API. It is also my first time outputting XML with PHP/SQL and then processing the results.

I am using this as my guide >> http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html

My problem is when I use a SQL table containing German addresses/characters, it doesn't work -- I get this error thrown to me

This page contains the following errors:

error on line 2 at column 1: Extra content at the end of the document 

..and examining the page source I see this...

<b>Warning</b>:  DOMElement::setAttribute() [<a href='domelement.setattribute'>domelement.setattribute</a>]: string is not in UTF-8 in <b>/Applications/MAMP/htdocs/guruTest/guruTest.php</b> on line <b>46</b><br />
<br />
<b>Warning</b>:  DOMDocument::saveXML() [<a  href='domdocument.savexml'>domdocument.savexml</a>]: output conversion failed due to conv  error, bytes 0xDF 0x65 0x20 0x33 in <b>/Applications/MAMP/htdocs/guruTest/guruTest.php</b>  on line <b>52</b><br />
<?xml version="1.0" ?>
<markers>

Using the same PHP/SQL but switching to a database of English addresses, it works fine. So it seems the problem is the German characters in my German addresses.

I had originally started with the code pretty much as you see on code.google.com. In that example, no charset is given for the XML document.

Here is where I think the problem is

$result = mysql_query($query);
if (!$result) {
  die("Invalid query: " . mysql_error());
}

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

// Iterate through the rows, adding XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  $node = $dom->createElement("marker");
  $newnode = $parnode->appendChild($node);
  $newnode->setAttribute("name", $row['name']);

When I started seeing the errors with my German info I added encoding to the head declaration. Simply by doing this...

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

But my error-ridden XML output was still showing

<?xml version="1.0" encoding="ISO-8859-1"?>

.. so .. I clearly have absolutely no idea what I'm doing.

Anyone want to help out?


回答1:


Use this string function while you pass the variable:

utf8_encode($variable);


来源:https://stackoverflow.com/questions/8127225/charset-in-xml-output-from-sql-german-characters-where-am-i-going-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!