第6行第1行的错误:仅在文档开头允许XML声明

nit*_*man 3 php xml

我正在尝试构建一个XML文件.但坚持错误.我包括代码和错误.提前致谢.

    <?php 
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("comrade") or die(mysql_error()); 
    $data = mysql_query("SELECT * FROM support WHERE DATE(`date`) = CURDATE()") 
    or die(mysql_error());
    $xml = new SimpleXMLElement('<xml/>');
    while($info = mysql_fetch_array( $data )) 
     { 
      $support = $xml->addChild('support');
      $support->addChild('cus_name',$info['com_name']);
      $support->addChild('ser_type',$info['ser_type']);
     } 

     Header('Content-type: text/xml');
     print($xml->asXML());
     ?>
Run Code Online (Sandbox Code Playgroud)

它显示以下错误

     Below is a rendering of the page up to the first error.
Run Code Online (Sandbox Code Playgroud)

Eli*_*iss 11

简而言之,你应该这样做:

ob_clean(); //Clean (erase) the output buffer
print($xml->asXML());
Run Code Online (Sandbox Code Playgroud)

解释:如果任何包含的文件打印出新行,您将获得

第6行第2行的错误:仅在文档开头允许XML声明

这可能是因为在某些文件中<?php标签在空行后开始.

因此,不要搜索导致错误的文件,只需清理输出缓冲区即可.