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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21404686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:07:51  来源:igfitidea点击:

XML Parsing Error: XML or text declaration not at start of entity

phpxmlrss

提问by user3095193

I have this error in my rss.php

我的 rss.php 中有这个错误

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

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>

The

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

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

is show between the left side of the page and the ?xml line.

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

In my rss.php I have

在我的 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;
?>

回答by Matt

You have a lot of white space before the declaration. You need to remove it. This can be caused by having closing PHP tags followed by spaces or tabs or new line at the end of included files. You can prevent this by just not closing the PHP tags.

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

Second look, remove all the closing tags at the top of your document:

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

<?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;

Also, just a note, the mysql_* functions are deprecated due to security problems. You may want to look at mysqli and PDO

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

回答by Vivek

I just remove the space from the top of wp-config file and its works for me... This could be a problem with the XML parsing White-Space.

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

回答by Sachith

I'm also faced this problem, remove all unwanted line spaces in your code,i mean remove all unwanted line spaces before and after the phptags. There can be more reasons but 95% this is the reason of blank space after or before phptags. <?php ?>.

我也面临这个问题,删除代码中所有不需要的行空间,我的意思是删除php标签前后所有不需要的行空间。可能有更多原因,但 95% 这是php标签前后空白的原因。<?php ?>.