Results 1 to 4 of 4

Thread: [RESOLVED] Delete Node In XML File

  1. #1
    Join Date
    Mar 2012

    resolved [RESOLVED] Delete Node In XML File

    I wonder whether someone may be able to help please.

    I've put together this page http://www.mapmyfinds.co.uk/development/deletetest.php which allows users to view a gallery of their uploaded images.

    Upon upload, each physical image is saved in the following file structure:
    The image details i.e description, filename etc are saved in an XML file called 'files.xml' which is in the same location as the images. An extract of which can be seen below.

     <?xml version="1.0" encoding="utf-8" ?> 
    - <files>
      <file name="_47061196_greatbritainjpg.jpg" source="_47061196_greatbritainjpg.jpg" size="227505" originalname="_47061196_greatbritainjpg.jpg" description="No description provided" userid="1" locationid="1" /> 
      <file name="stag.jpg" source="stag.jpg" size="21341" originalname="stag.jpg" description="No description provided" userid="1" locationid="1" /> 
    You will see under each image there is an icon which allows the user to delete the image. This done via the following:

    Icon Click Event

    <script type="text/javascript"> 
    		Galleria.ready(function() {
    		"<span class='btn-delete ui-icon ui-icon-trash'></span>"); 
    		$(".btn-delete").live("click", function(){
    		var img = $(this).closest(".galleria-image").find("img"); 
    		// send the AJAX request
    		url : 'delete.php',
    		type : 'post',
    		data : { image : img.attr('src') },
    		success : function(){
    		alert('Deleting image... ');
    		return false;

    PHP Code:

    if (!empty($_POST)) { 
    $image $_POST['image']; 

    if (
    file_exists($image)) { 

    $doc = new DOMDocument

    $thedocument $doc->documentElement

    $list $thedocument->getElementsByTagName('files'); 

    $nodeToRemove null
    foreach (
    $list as $domElement){ 
    $attrValue $domElement->getAttribute('file_name'); 
    if (
    $attrValue == 'image') { 
    $nodeToRemove $domElement

    if (
    $nodeToRemove != null

    With this, I can succesfully delete the physical image, but what I'm having great difficulty in doing is deleting the corresponding child node in the XML file.

    I really quite new to dealing with XML but I've been working on this for weeks now, and I'm still no further forward.

    I just wondered whether someone could perhaps have a look at this please and let me know where I'm going wrong.

    Many thanks and regards

  2. #2
    Join Date
    Oct 2008
    A few things jump out at me here. First, in the xml snippet you provided I do not see anywhere that you have an attribute by the name of 'file_name' which you are looking for in your php file:
    $attrValue = $domElement->getAttribute('file_name');
    So I believe you intend that to be:
    $attrValue = $domElement->getAttribute('name');
    Then the next thing is this:
    if ($attrValue == 'image') {
    $nodeToRemove = $domElement;
    Rather than 'image' I believe you intend that to be $image

  3. #3
    Join Date
    Mar 2012
    Hi, thank you very much for taking the time to respond to my post, it is greatly appreciated.

    You can probably tell, that I'm a complete beginner when it comes to XML.

    I've made the changes you suggested, but unfortunately although the deletion of the physical image still works, I'm unable to get the corresponding node to be deleted.

    I just wondered whether it would be at all possible for you to offer a little more guidance please.

    Many thanks and regards

  4. #4
    Join Date
    Mar 2012

    Realising that the 'image' value carried forward the whole file path rather than just the file name of the image, I had to first break the file path down, then open the file and make the necessary changes to the XML file.

    My solution is as follows:

    PHP Code:
    <?php session_start(); 

    if (!empty(
    $_POST)) {   
    $originalimage $_POST['image'];   

    if (
    file_exists($originalimage)) {   

    $path_parts pathinfo($originalimage);
    $dir $path_parts['dirname']; 
    $basename $path_parts['basename'];



    $dom = new DomDocument();
    $dom->load('UploadedFiles/' $_SESSION['userid'] . '/' $_SESSION['locationid'] . '/' 'files.xml'); 

    $root $dom->documentElement;

    $images $root->getElementsByTagName("file");
    $length $images->length;

    //Iterate backwards by decrementing the loop counter 
    for ($i=$length-1;$i>=0;$i--)
    $p $images->item($i); 
    $pid $p->getAttribute("name");

    if (
    $pid == $newimage)

    $parent $p->parentNode;

    $dom->save('UploadedFiles/' $_SESSION['userid'] . '/' $_SESSION['locationid'] . '/' 'files.xml'); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles