-3

我不知道出了什么问题,我正在使用同一个脚本来获取另一个 XML,它工作得很好。这是脚本:

<?php
header("Content-type: text/xml");
echo "<?xml version=\"1.0\" encoding=\"LATIN-1\"?>";
echo "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">";
echo "<plist version=\"1.0\">";

function getXML($sql="SELECT IDCargo FROM database.table"){

    $conn   = mysql_connect ("myserverurl.com", "user", "psw");
    $db     = mysql_select_db("database");
    $result = mysql_query($sql, $conn);
    $column = "";
    echo "<array>";

    while($row = mysql_fetch_assoc($result)){
        $column .= "<dict>";
        foreach ($row as $key => $value){
            $column .= "<key>$key</key>";
            $column .= "<string>$value</string>";
        }
        $column .= "</dict>";
    }

    echo $column;
    echo "</array>";
    echo "</plist>";
}
getXML("SELECT IDCargo as ID_CARGO, SequencialDoCandidato as NUMERO_SEQ_CANDIDATO, NomeDoCandidato AS NOME_CANDIDATO, NumeroDoCandidato as NUMERO_CANDIDATO, NomeDoPartido AS SIGLA_PARTIDO, Estado as SIGLA_ESTADO,IDUnidadeEleitoral AS ID_CIDADE_CANDIDATO, UnidadeEleitoral AS CIDADE_CANDIDATO FROM database.table");
mysql_close();
?>

它带来了这个错误:

This page contains the following errors:

error on line 1 at column 538: Encoding error
Below is a rendering of the page up to the first error.

ID_CARGO11NUMERO_SEQ_CANDIDATO10000002965NOME_CANDIDATOAGRECINO DE SOUSANUMERO_CANDIDATO13SIGLA_PARTIDOPTSIGLA_ESTADOACID_CIDADE_CANDIDATO1120CIDADE_CANDIDATO

任何人都可以看到任何东西吗?请在这里给我一些帮助。我从一个运行良好的脚本那里得到了脚本。

4

2 回答 2

1

如果我阅读了您的代码,我可以看到:

echo "<?xml version=\"1.0\" encoding=\"LATIN-1\"?>";

所以我猜你想输出 LATIN-1 字符集。您需要在任何地方指定此字符集:

1/ 您可以尝试在 http 标头上指定编码:

header("Content-type: text/xml; charset=ISO-8859-1");

2/您还可以在数据库客户端上设置字符集:

mysql_set_charset('latin1', $db);
于 2012-09-19T18:32:32.017 回答
0

您的输出很可能包含默认编码不支持的编码中的一个或多个字符。

更新您使用的编码,或转义任何不适合编码的字符。php 函数将htmlentities是一个好的开始。

于 2012-09-19T18:37:36.660 回答