【问题标题】:Encoding error "entity not defined" in XML RSS feed with PHP MySQL query使用 PHP MySQL 查询在 XML RSS 提要中编码错误“实体未定义”
【发布时间】:2014-08-02 12:21:42
【问题描述】:

我正在尝试基于 MySQL 查询的 PHP 输出开发我自己的 XML RSS 提要。但是,即使我已将所有内容设置为 UTF8 编码和字符集(数据库连接、xml 版本、utf8_encode),我仍不断收到我的 DB 内容字段中所有 ASCII 字符的“实体 X 未定义”错误消息,但没有任何内容可以消除错误:

<?php
$connection = mysqli_connect( .... )
$connection->set_charset("utf8");
header("Content-type: text/xml; charset=utf-8");
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<rss version="2.0">';
$query = mysqli_query($connection,"SELECT * FROM news ORDER BY pubdate DESC LIMIT 10");
while($row = mysqli_fetch_assoc($query)){
$title = utf8_encode($row['title']);
$content = utf8_encode($row['content']);
echo '<item><title>'.$title.'</title>';
echo '<description>'.$content.'</description></item>';
} // end while
echo '</channel>';
echo '</rss>';
?>

我错过了什么?

非常感谢!

【问题讨论】:

  • 请在问题中包含生成的 XML 代码(或足够长的摘录)。
  • 您遇到的问题是您手动创建 XML,而不是使用为创建 XML 而创建的库,例如 SimpleXML 或 DOMDocument。这些库负责对您从数据库中获取的 UTF-8 编码字符串进行正确的 XML 编码。

标签: php mysql xml character-encoding rss


【解决方案1】:

我也遇到了同样的问题,用下面的链接修复了

我想你正在寻找这个

http://help.simplytestable.com/errors/html-validation/general-entity-x-not-defined-and-no-default-entity/

【讨论】:

  • 我是上述链接帮助文章的作者。很高兴看到它有用!
【解决方案2】:

您必须转义 $title$content 变量。检查htmlspecialchars()

要获得更好的解决方案,请使用DOM 创建 XML。这将确保您创建一个有效的 XML。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-20
    • 2011-09-18
    • 2012-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2021-12-22
    相关资源
    最近更新 更多