How to delete xml node using php


Create delete.php file and add the following code
<?php
function delete_book_id($id, $filename = 'loaned.xml')
{
$data = simplexml_load_file($filename);
for($i = 0, $length = count($data->resource->book); $i < $length; $i++)
    {
    if($data->resource->book[$i]->ID == $id)
        {
        unset($data->resource->book[$i]);
        break;
        }
    }
    file_put_contents($filename, $data->saveXML());
    echo $deletingbook." Deleted !!!";
}
//SAMPLE USAGE
$deletingbook = $_GET['bid'];

delete_book_id ($deletingbook);
 header("Location: {$_SERVER['HTTP_REFERER']}");

//delete_book_id('Book 1');
?>

Comments

chrisdivyak said…
Is there any way to delete the node using a unique xml filename?

Example: $filename = "xml/$title.xml";

I can't seem to get it to work on my server and keep getting 500 internal errors. Any help would be appreciated. This is exactly what I was looking for in tutorials so thank you!
chrisdivyak said…
Is there any way to delete with a dynamic file name?

Example: I want to use $filename = "xml/$title.xml"; and keep getting 500 internal errors.

Thank you for this tutorial because it's exactly what I needed for a project I am working on. Just trying to get it working with some of my dynamic elements. Thanks in advance.

Popular posts from this blog

CSS Mouse Cursors and Custom Cursors

Block right click or disable keyboard on web pages