我試圖刪除與從XML文件特定ID的用戶,但面臨以下錯誤:錯誤而通過PHP函數從XML移除元素
Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in delUser.php
XML文件:
<currentUsers>
<user id="101" firstName="Klashinkof" p2p="Yes" priority="Low"/>
<user id="102" firstName="John" p2p="Yes" priority="High"/>
</currentUsers>
代碼:
<?php
$id=101; //Test
// SETUP $doc
$doc = new DomDocument("1.0");
$doc->preserveWhiteSpace = FALSE;
$doc->validateOnParse = true;
$doc->Load('currUsers.xml');
//REMOVE ID
$user= $doc->getElementByID($id);
$users= $doc->documentElement;
if ($oldPerson = $users->removeChild($user)) {
// worked
echo "DELETED user {$id}";
} else {
return "Couldn't remove $id listing";
}
$doc->save(curr.xml);
?>
'的DOMDocument :: getElementById' - >返回一個DOMElement或NULL,如果未找到該元素。所以它看起來像'$ doc-> getElementByID($ id);'不能通過id找到元素。 – 2011-02-24 08:34:22