2015-12-18 131 views
0

我试图从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> 

回答

0

由于flight是一个$item,你应该删除$item而不是$node

$item->parentNode->removeChild($item); 
0

您可以使用parentNode多次如你所需,尽管显然它需要你确信你的XML结构不会改变。在这种特殊情况下,你只需要一个级别上的上升,这样的:

$node->parentNode->parentNode->removeChild($node->parentNode); 

但看到你在$item正在建立一个循环是已经,只是删除直接:

$item->parentNode->removeChild($item); 
1

考虑使用XSLT,它是重构XML文件的专用语言。 PHP维护一个XSLT 1.0处理器。因此,不需要foreach循环或if逻辑条件,因为样式表脚本将处理这种处理并且非常有效。你甚至可以嵌入在PHP脚本动态传递$id变量:

PHP嵌入式XSLT

// Retrieve imagepath value 
$id = $_GET['imagepath']; 

// Load the XML source 
$doc = new DOMDocument(); 
$doc->load('Flights.xml'); 

// Parse XSLT 
$xsl = new DOMDocument; 
$xslstr = '<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
      <xsl:output version="1.0" encoding="UTF-8" indent="yes" /> 
      <xsl:strip-space elements="*"/> 

      <!-- Identity Transform --> 
      <xsl:template match="@*|node()"> 
       <xsl:copy> 
       <xsl:apply-templates select="@*|node()"/> 
       </xsl:copy> 
      </xsl:template> 

      <xsl:template match="flight[imagepath=\''.$id.'\']"/> 
      </xsl:transform>'; 

$xsl->loadXML($xslstr); 

// Configure the transformer 
$proc = new XSLTProcessor; 
$proc->importStyleSheet($xsl); 

// Transform XML source 
$newXml = $proc->transformToXML($doc); 

// Save output to file 
$xmlfile ='Output.xml'; 
file_put_contents($xmlfile, $newXml); 

输出

<?xml version="1.0" encoding="UTF-8"?> 
<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> 
</ArrivingFlights>