php - XML 解析错误 : XML or text declaration not at start of entity

标签 php xml rss

我的 rss.php 中有这个错误

XML Parsing Error: XML or text declaration not at start of entity Location: http://blah.com/blah/blah/site/rss.php Line Number 1, Column 3:
and this is shown underneath the error

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"><channel><item><title>xzfbcbcxv 123</title><description><p>Description for the rss feed</p></description></item></channel></rss>

----------------^

显示在页面左侧和 ?xml 行之间。

在我的 rss.php 中有

<?php header("Content-type: text/xml"); ?>
<?php include("includes/database.php");?>
<?php global $_TWITTER_TABLE, $_HTTP_ADDRESS, $_NEWS_TABLE; ?>
<?php $str = '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<?php $str .= '<rss version="2.0">';?>
<?php
$str.='<channel>';
$sql = "SELECT * FROM $_NEWS_TABLE LIMIT 5";
$result = mysql_query($sql) or die ($sql."".mysql_error());
    while($row = mysql_fetch_object($result)){
        $str.= '<item>';
                $str.= '<title>'.$row->title. '</title>';
                $str.= '<description>'.$row->content. '</description>';
        $str.= '</item>';
}
$str.= '</channel>';
?>
<?php $str .= '</rss>'; 
    echo $str;
?>

最佳答案

声明前有很多空白。您需要将其删除。这可能是由于包含文件末尾的 PHP 结束标记后跟空格或制表符或换行符造成的。您可以通过不关闭 PHP 标记来防止这种情况。

再看一遍,删除文档顶部的所有结束标记:

<?php // make sure that this is the first character in the file, no spaces before it
    header("Content-type: text/xml");
    include("includes/database.php");
    global $_TWITTER_TABLE, $_HTTP_ADDRESS, $_NEWS_TABLE;
    $str = '<?xml version="1.0" encoding="UTF-8"?>';
    $str .= '<rss version="2.0">';
    $str.='<channel>';
    $sql = "SELECT * FROM $_NEWS_TABLE LIMIT 5";
    $result = mysql_query($sql) or die ($sql."".mysql_error());
    while($row = mysql_fetch_object($result)){
        $str.= '<item>';
        $str.= '<title>'.$row->title. '</title>';
        $str.= '<description>'.$row->content. '</description>';
        $str.= '</item>';
    }
    echo $str;

此外,请注意,由于安全问题,mysql_* 函数已被弃用。你可能想看看 mysqli 和 PDO

关于php - XML 解析错误 : XML or text declaration not at start of entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21404686/

相关文章:

php - 将 "the loop"从 Wordpress 添加到任何 html/php 模板的简单方法(不是为 wordpress 制作的)

php - 如何将变量设置为 select 语句上的默认选择以及从数组中列出选择

Java(JAXP) 和 XSLT : Overwriting the XML file

xml - Hadoop Hive XPaths仅返回阵列的一部分

php - 在 xslt 转换中保留节点

PHP:使用特殊字符 explode

php - Mysql,更新不在数组php中的地方

rss - 你如何添加原子 :link with rel ="self" to an rss document?

google-chrome - Chrome 扩展 : How do I poll RSS Feeds to see if there is new content and display that as a number on the extension icon

java - 关于使用 Informa RSS 库的问题