2014-04-04 46 views
0

我怎么删除父节点与XML PHP和保存更改XML查找节点家长和删除simplexml的PHP

我怎么努力

<element> 
    <feed id=" 9 "> 
     <title>test</title> 
     <table>feeds</table> 
     <link/> 
     <feed_type>API</feed_type> 
     <affiliate/> 
     <mwst>ja</mwst> 
     <tld>DE</tld> 
     <query/> 
    </feed> 
</element> 
public function deleteNode($feed_id){ 

    //find feed on attribute ID 
    $node = $this->xml->xpath('//feed[@id =' . $feed_id . ' ]'); 
    //find the parent and delete thenode 
    $element = $node[0]->xpath('parent::*'); 
    // ??unset($element[0]); 
    $this->xml->asXML(SITE_ROOT . '/xml/feed_config.xml'); 
    exit(); 
} 
+1

您可以使用'SimpleXml'来删除节点,请参阅http://stackoverflow.com/questions/13002313/php-simplexml-delete-please-tell-me-why-the-第一个变种是不工作的 – michi

回答

0

编辑#1:

(感谢@michi指出unset足以删除Simpl中的节点EXML)

如果你想删除<element>,因为我以前认为不<feed>,你可以这样做:

function deleteNode($feed_id) 
{ 
    $parent=$this->xml->xpath('//*[feed[@id=" '.$feed_id.' "]]'); 
    unset($parent[0][0]); 
} 

Online demo
(Works为PHP> = 5.2.2)


Original post

,您可能需要DOM此:

function deleteNode($feed_id) 
{ 
    $node=$this->xml->xpath('//feed[@id=" '.$feed_id.' "]'); 
    $node=dom_import_simplexml($node[0]); 
    $parent=$node->parentNode; 
    $parent->removeChild($node); 
    $this->xml=simplexml_import_dom($parent->ownerDocument); 
} 

Online demo
(注意,因为它不是在演示的一类,我用了一个global懒惰仿真的目的。在您的实际代码中避免使用global。)

+0

只是问最后一部分似乎并不工作我还有我该怎么坐骑? – fefe

+0

@fefe哎呀,我以为你想删除''。检查我的更新。 – Passerby