删除多个带属性的xml笔记?

时间:2013-09-11 15:22:17

标签: php xml dom

我有这个代码

$doc = new DOMDocument; 
$doc->load('Clans/' . $_POST['s']);

$thedocument = $doc->documentElement;

//this gives you a list of the messages
$list = $thedocument->getElementsByTagName('member');
foreach($_POST['delete_unit'] as $unit_id){
$nodeToRemove = null;
foreach ($list as $domElement){
  $attrValue = $domElement->getAttribute('id');
  if ($attrValue == $unit_id) {
    $nodeToRemove = $domElement; //will only remember last one- but this is just an example :)
  }
}
}

//Now remove it.
if ($nodeToRemove != null)
$thedocument->removeChild($nodeToRemove);

$doc->saveXML('Clans/' . $_POST['s']);

POST ['delete_unit']是ids数组 为什么这不起作用或有更好的方法来做到这一点

错误代码PHP Catchable致命错误:传递给DOMDocument :: saveXML()的参数1必须是DOMNode的实例,在

中给出的字符串

1 个答案:

答案 0 :(得分:1)

在最后一行,我认为你正在寻找

file_put_contents('Clans/' . $_POST['s'], $doc->saveXML());

如果没有传递参数,saveXML()将整个文档作为字符串返回。

您还需要考虑清理表单输入!