执行下面的代码(减去数据库调用)后,我收到“第 7 列第 331 行错误:文档末尾的额外内容”错误。我浏览了这些论坛,但找不到解决方案。我没有任何随机字符或任何应该添加额外空格的代码......有什么想法吗?
<?php
header('Content-type: text/xml');
mysql_connect("localhost", "---", "---");
mysql_select_db("---");
$query = "SELECT title FROM table";
$result = mysql_query($query);
$xml = new XMLWriter();
$xml->openURI("php://output");
$xml->startDocument();
$xml->setIndent(true);
$xml->writeRaw('<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9">');
$xml->startElement('url');
while ($row = mysql_fetch_assoc($result)) {
if(!empty($row)){
$title = $row['title'];
$xml->startElement('loc');
$xml->writeRaw('http://domain.com/article/');
$xml->endElement();
$xml->startElement('news:news');
$xml->startElement("news:publication");
$xml->startElement("news:name");
$xml->writeRaw('Name');
$xml->endElement();
$xml->startElement("news:language");
$xml->writeRaw('en');
$xml->endElement();
$xml->endElement();
$xml->startElement('news:title');
$xml->writeRaw($title);
$xml->endElement();
$xml->endElement();
}
}
$xml->endElement();
$xml->flush();