我想用PHP DomDocument类创建站点地图。 当我试图从Kohana的模型数据库获取数据,它显示了一个错误的内容:PHP DomDocument错误 - XML声明不在文档开始处
XML声明不是在文件
当我删除这两条线与模型的访问开始 - 一切正常,出了什么问题?我需要这些数据来创建我的网址。
我使用这个功能:
public function sitemap()
{
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElementNS("http://www.sitemaps.org/schemas/sitemap/0.9","urlset");
$r->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance',
'xsi:schemaLocation',
'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'
);
$doc->appendChild($r);
$model = new Data_Model; // THESE TWO LINES CAUSES ERROR
$arrayofdata = $model->get_all();
for($i=0; $i<10; $i++)
{
$b = $doc->createElement("url");
$loc = $doc->createElement("loc");
$loc->appendChild($doc->createTextNode('www.example.com'));
$b->appendChild($loc);
$priority = $doc->createElement("priority");
$priority->appendChild(
$doc->createTextNode('1.0')
);
$b->appendChild($priority);
$r->appendChild($b);
$changefreq = $doc->createElement("changefreq");
$changefreq->appendChild(
$doc->createTextNode('Daily')
);
$b->appendChild($changefreq);
$lastmod = $doc->createElement("lastmod");
$lastmod->appendChild(
$doc->createTextNode(date('Y-m-d'))
);
$b->appendChild($lastmod);
$r->appendChild($b);
}
$output = $doc->saveXML();
header("Content-type:text/xml");
echo $output;
}
你确定这些行不会导致PHP的通知或警告的麻烦? – 2013-03-11 21:51:41