我一直在学习使用PHP创建XML页面的教程。以下是代码。
<?
header('Content-type: text/xml');
mysql_connect('localhost','root','admin');
mysql_select_db('test');
$sql = "Select * from `books`";
$q = mysql_query($sql) or die(mysql_error());
$dom = new DOMDocument();
$datas = $dom->createElement('datas');
$dom->appendChild($datas);
while($r = mysql_fetch_array($q)){
$id = $dom->createElement('id');
$idText = $dom->createTextNode($r['id']);
$id->appendChild($idText);
$title = $dom->createElement('title');
$titleText = $dom->createTextNode($r['title']);
$title->appendChild($titleText);
$author = $dom->createElement('author');
$authorText = $dom->createTextNode($r['author']);
$author->appendChild($authorText);
$book = $dom->createElement('book');
$book->appendChild($id);
$book->appendChild($title);
$book->appendChild($author);
$datas->appendChild($book);
}
$xmlString = $dom->saveXML();
echo $xmlString;
?>
但由于某些原因,当我运行此操作时,我收到一个错误 - “第1行第5行的错误:文档末尾的额外内容”