0
我正在尝试编写一个更新网站的网站地图的功能,或者从技术上将条目添加到sitemap.xml文件中。一个谷歌地图的标准结构,可以在这里看到:http://www.sitemappro.com/google-sitemap.html使用PHP DOMDocument()更新XML文件时出现问题;
以下是该函数的代码:
function AddEntry($loc,$lastmod,$changefreq,$priority){
$xmlDoc = new DOMDocument();
$xmlDoc->load("sitemap.xml");
$url []=array('loc' => $loc, 'lastmod' => $lastmod, 'changefreq'=> $changefreq, 'priority' =>$priority);
$r=$xmlDoc->createElement("url");
$xmlDoc->appendChild($r);
foreach($url as $key=>$value)
{
$r->appendChild($xmlDoc->createElement($key))->appendChild($xmlDoc->createTextNode($value));
}
$xmlDoc->save();
}
上面的代码不工作并给予此错误:
“致命错误:未收集异常'DOMException',并在'...'中显示“无效字符错误”消息“
您可以通过更正我的代码来获得帮助吗?提前致谢。
这是哪一行发生?推测foreach循环内的appendChild? –