i'm trying to remove a specific node from XML using PHP. This is the structure of the XML :
<ArrivingFlights>
<flight>
<to>Michelle</to>
<from>Brianna xx</from>
<imagepath>0001.jpg</imagepath>
<templateStyle>template1</templateStyle>
<time>17:00</time>
<date>18/12/15</date>
</flight>
<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>
</ArrivingFlights>
I have a PHP file that im using to get a FileName so i can remove a node based on that file name and replace it. This is my PHP :
<?php
$id = $_GET['imagepath'];
$xmldoc = new DOMDocument();
$xmldoc->load('newcoke.xml');
$root = $xmldoc->documentElement;
$fnode = $root->firstChild;
// we retrieve the chapter and remove it from the book
$items = $xmldoc->getElementsByTagName('flight');
foreach ($items as $item){
$node = $item->getElementsByTagName('imagepath')->item(0);
if ($node->nodeValue == $id){
$node->parentNode->removeChild($node);
}
}
$xmldoc->save('newXmlFile.xml');
?>
This SORT OF works, when i look at newFile.xml it is removing "ImagePath" but i wanted it to remove that whole "flight" node, so basically its parent. This is the result i get if i pass it 0001.jpg :
<flight>
<to>Michelle</to>
<from>Brianna xx</from>
<templateStyle>template1</templateStyle>
<time>17:00</time>
<date>18/12/15</date>
</flight>
<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>