orionvictor Posted December 7, 2014 Share Posted December 7, 2014 XML file: <?xml version="1.0" ... ><database xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <record> <id>1</id> <name></name> </record> <record> ... </record></database> I want to delete the record where <id> equals $_GET['id']. I tried many suggested implementations based on xml or simplexml, without result. Came across a very simple workaround (it should omit the record where id=$id), but can someone help me to taylor it to my xml scheme? <?php$file = "database.xml";$id = $_GET['id'];echo "DELETING: ".$id;$xml = simplexml_load_file($file);$str = "<?xml version=\"1.0\"?><database>";foreach($xml->children() as $child){if($child->getName() == "id") {if($id == $child['name']) {continue;} else {$str = $str.$child->asXML();}}}$str = $str."</database>";echo $str;$xml->asXML("database_backup.xml");$fh = fopen($file, 'w') or die("can't open file");fwrite($fh, $str);fclose($fh);?> Quote Link to comment https://forums.phpfreaks.com/topic/292954-simple-xml-delete-node-by-id/ Share on other sites More sharing options...
QuickOldCar Posted December 8, 2014 Share Posted December 8, 2014 Using GET parameter as id http://mysite.com/delete-xml-id.php?id=3 delete-xml-id.php <?php function delete_record($id, $file) { $xml = new DOMDocument(); $xml->load($file); $record = $xml->getElementsByTagName('record'); foreach ($record as $person) { $person_id = $person->getElementsByTagName('id')->item(0)->nodeValue; //$person_name=$person->getElementsByTagName('name')->item(0)->nodeValue; if ($person_id == $id) { $id_matched = true; $person->parentNode->removeChild($person); break; } } if ($id_matched == true) { if ($xml->save($file)) { return true; } } } $file = "database.xml"; if (file_exists($file)) { if (isset($_GET['id']) && ctype_digit(trim($_GET['id']))) { $id = trim($_GET['id']); if (delete_record($id, $file)) { echo "$id removed"; } else { echo "$id not removed"; } } else { echo "id missing"; } } else { echo "$file missing"; } ?> database.xml <?xml version="1.0" encoding="UTF-8"?> <database xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <record> <id>1</id> <name>mark</name> </record> <record> <id>2</id> <name>bob</name> </record> <record> <id>3</id> <name>jane</name> </record> <record> <id>4</id> <name>mary</name> </record> <record> <id>5</id> <name>mary</name> </record> </database> Quote Link to comment https://forums.phpfreaks.com/topic/292954-simple-xml-delete-node-by-id/#findComment-1498922 Share on other sites More sharing options...
orionvictor Posted December 8, 2014 Author Share Posted December 8, 2014 The code works as a champ! Easy solution with parentNode->removeChild functionality. Hope it might help someone else too. Quote Link to comment https://forums.phpfreaks.com/topic/292954-simple-xml-delete-node-by-id/#findComment-1498990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.