【问题标题】:XML Parsing Error: XML or text declaration not at start of entity in phpXML 解析错误:XML 或文本声明不在 php 中实体的开头
【发布时间】:2012-12-20 15:46:44
【问题描述】:

您好,我在 php 中生成一个 xml 文件,但出现错误

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

我的代码是---

<?php require_once('../../settings.php');
header("Content-type:text/xml");
$dom=new DOMDocument('1.0'); 
$dom->formatOutput = true;
$id=(int)$_GET['imageid'];
 $query="select * from tbl_image_gallery where imageId='$id' ORDER BY gallId DESC ";
$select=mysql_query($query);
$content = $dom->appendChild($dom->createElement('content'));
while($res=mysql_fetch_array($select))
{
    $image = $content->appendChild($dom->createElement('image'));
    $small_image_path = $image->appendChild($dom->createElement('small_image_path'));
    $small_image_path->appendChild($dom->createTextNode("image_gallery/load/images/small/".$res['image'])); 
    $big_image_path = $image->appendChild($dom->createElement('big_image_path'));
    $big_image_path->appendChild($dom->createTextNode("image_gallery/load/images/big/".$res['image']));
    $description = $image->appendChild($dom->createElement('description'));
    $description->appendChild($dom->createCDATASection($res['description']));
 }   
echo $test1 = $dom->saveXML(); 
?>

在google上搜索后发现是之前空间的问题

我删除了空间但没有得到预期的结果。

【问题讨论】:

    标签: php


    【解决方案1】:

    删除下面的行并尝试错误消息是否消失那么您肯定有空间或之前在settings.php文件中输出的其他内容。

      header("Content-type:text/xml");
    

    检查 settings.php 文件并删除空格(如果有)。请注意,如果 settings.php 文件还包含其他一些文件,您还必须检查这些文件中的空间。

    【讨论】:

      【解决方案2】:

      echo $test1 = $dom-&gt;saveXML(); 之前使用ob_clean();

      【讨论】:

        猜你喜欢
        • 2014-02-19
        • 2014-08-03
        • 1970-01-01
        • 1970-01-01
        • 2011-12-30
        • 1970-01-01
        • 2013-11-13
        • 2011-11-06
        • 1970-01-01
        相关资源
        最近更新 更多