【问题标题】:XML Parsing Error: XML or text declaration not at start of entityXML 解析错误:XML 或文本声明不在实体的开头
【发布时间】:2014-02-19 16:45:06
【问题描述】:

我的 rss.php 中有这个错误

XML 解析错误:XML 或文本声明不在实体的开头 地点:http://blah.com/blah/blah/site/rss.php 第 1 行,第 3 列:
这显示在错误下方

<?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 xml rss


    【解决方案1】:

    我也遇到了这个问题,删除代码中所有不需要的行空格,我的意思是删除php 标签之前和之后的所有不需要的行空格。 可能有更多原因,但 95% 这是php 标记之后或之前的空格的原因。 &lt;?php ?&gt;.

    【讨论】:

      【解决方案2】:

      我只是从 wp-config 文件顶部删除空间,它对我有用... 这可能是 XML 解析 White-Space 的问题。

      【讨论】:

        【解决方案3】:

        声明前有很多空白。您需要将其删除。这可能是由于 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

        【讨论】:

        • 谢谢你的工作,现在我只需要我的循环工作:-D
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-03
        • 1970-01-01
        • 2011-11-06
        • 2011-12-30
        • 2013-11-13
        • 1970-01-01
        相关资源
        最近更新 更多