XML解析错误:XML或文本声明不在实体的开头

use*_*193 4 php xml rss

我的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>
Run Code Online (Sandbox Code Playgroud)

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

显示在页面左侧和?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;
?>
Run Code Online (Sandbox Code Playgroud)

Mat*_*att 9

宣言前你有很多空格.你需要删除它.这可能是因为关闭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;
Run Code Online (Sandbox Code Playgroud)

另外,只是注意,由于安全问题,不推荐使用mysql_*函数.你可能想看看mysqli和PDO