domdocument在任何地方都没有很好地记录(包括SO),所以我想我会问这里的问题。我有一个PHP / XML博客,并希望让博主能够一次删除多个博客文章。当两个帖子ID出现故障或不相互跟随时,以下代码有效。
示例 - 当选择post id 1和post id 4时,它们都会删除。但是当选择后1和后2时,只删除2。每次行为都是一致的。有没有人遇到过这个问题,或者在我的代码中看到会引起这种情况的事情?
伪XML:
<root>
<blog id=1><copy>Some copy</copy></blog>
<blog id=2><copy>Another block of copy</copy></blog>
<blog id=3><copy>Last chance copy</copy></blog>
</root>
用于删除博客帖子的PHP代码(较大类的片段):
/**
* method usage: $objVar->deletePost($param)
* delete client selected xml[blog] entries
* @param array, $params : assign $_POST array as argument
* @return string, XML
*/
public function deletePost($params) {
foreach ($params as $value){ // assign $param['delete[n]'] to array
$pid=$value; // array[n][id]
}
$xml = new DOMDocument(); // instance of current xml
$xml->load($this->fp); // path to file
$xml->preserveWhiteSpace = false;
//define xpath object, called with query() line 273
$xpath = new DOMXpath($xml);
//create temporary DOM xml
$tempxml = $xml->documentElement;
$pnode = $xml->getElementsByTagName('blog');
foreach($pnode as $key){ //outter loop thru parent nodes
foreach($pid as $v){ //inner loop thru pid array
if ($key->getAttribute('id')==$v){ //if pid matches blog[id=n]
$path = "//root/blog[@id=".$v."]";
$nodelist = $xpath->query($path);
$removenode = $nodelist->item(0);
//remove the parent/children for matching ids
$oldxml = $tempxml->removeChild($removenode);
}
}
}
echo $xml->saveXML();
}
一如既往,提前谢谢