XML : Remove XML node using PHP depending on value

i'm trying to remove a specific node from XML using PHP. This is the structure of the XML :

  <ArrivingFlights>     <flight>      <to>Ger</to>      <from>Mammy xx</from>      <imagepath>0002.jpg</imagepath>      <templateStyle>template1</templateStyle>      <time>08:00</time>      <date>21/12/15</date>  </flight>  <flight>      <to>Ciara</to>      <from>Vikki xx</from>      <imagepath>0003.jpg</imagepath>      <templateStyle>template1</templateStyle>      <time>11:00</time>      <date>17/12/15</date>  </flight>    

This is my PHP file, it gets the filepath from javascript so i can search the XML for the imagepath node (as its unique). :

      <?php          $id = $_GET['imagepath'];    $doc = new DOMDocument;   $doc->load('newcoke.xml');    $thedocument = $doc->documentElement;    $list = $thedocument->getElementsByTagName('flight');      $nodeToRemove = null;  foreach ($list as $domElement){    $attrValue = $domElement->getAttribute('imagepath');    if ($attrValue == $id) {      $nodeToRemove = $domElement;     }  }    if ($nodeToRemove != null)  $thedocument->removeChild($nodeToRemove);    echo $doc->saveXML();         // echo json_encode($times);      ?>    

It is showing all the XML from the XML file but it isnt deleting any of the nodes. I need it to remove a node when the $id value matches the value of "imagepath"

No comments:

Post a Comment