我试图从PHP中使用XML从XML中删除特定的节点。这是XML的结构:PHP根据值删除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>
我有一个IM使用获得的文件名,所以我可以删除基于该文件名称的节点和替换它的PHP文件。这是我的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');
?>
这类的作品,当我看到newFile.xml被删除“的ImagePath”,但我希望它删除整个“飞行”的节点,所以基本上它的父。 这是结果我得到,如果我通过它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>