使用PHP DOMDocument()更新XML文件时出现问题;
我正在尝试编写一个函数来更新网站的站点地图,或者在技术上向sitemap.xml文件添加一个条目。谷歌网站地图的标准结构如下所示: 以下是该功能的代码:使用PHP DOMDocument()更新XML文件时出现问题;,php,xml,domdocument,Php,Xml,Domdocument,我正在尝试编写一个函数来更新网站的站点地图,或者在技术上向sitemap.xml文件添加一个条目。谷歌网站地图的标准结构如下所示: 以下是该功能的代码: function AddEntry($loc,$lastmod,$changefreq,$priority){ $xmlDoc = new DOMDocument(); $xmlDoc->load("sitemap.xml"); $url []=array('loc' => $loc, 'lastmod' => $
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();
}
上述代码不起作用,出现此错误:
致命错误:未捕获的异常“DomeException”,在
你能帮我修改一下密码吗?提前感谢。正如评论中指出的,问题应该在这一行
$r->appendChild($xmlDoc->createElement($key))->appendChild($xmlDoc->createTextNode($value));
很可能您正在向XML中添加一些url字符。问题是,您必须将它们放在一起,或者在$value周围加上一个部分。否则,生成的XML将无效,XML库将抛出此类异常
另外,我不记得该函数是否也适用于xml,我想是这样,但您需要对此进行调查。这发生在哪一行?大概是foreach循环中的appendChild?