I am trying to Delete a user with particular ID from an xml file but facing following error:
Argument 1 passed to DOMNode::removeChild() must be an instance of DOMNode, null given in delUser.php
XML file:
<currentUsers>
<user id="101" firstName="Klashinkof" p2p="Yes" priority="Low"/>
<user id="102" firstName="John" p2p="Yes" priority="High"/>
</currentUsers>
code:
<?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);
?>