2013-10-13 120 views
0

我需要刪除String中XML的前N個節點。如何刪除PHP中的XML節點

$xml = new SimpleXMLElement($xmlString, LIBXML_NOCDATA); 

    $node = $nodes->xpath("/data/event"); 
    $total_nodes = count($node); 

    $delete_n_rows = $total_nodes-$N; 

    for($i=0; $i<$delete_n_rows; $i++){ 
     if (! empty($node)) { 
      unset($node[i]); 
     } 
    } 

但是,該代碼不能在字符串中的這個xml中工作。

<data profile='color'><scale mode='month' today='February 2010'><x> 
<column><![CDATA[Monday]]></column> 
<column><![CDATA[Tuesday]]></column> 
<column><![CDATA[Wednesday]]></column> 
<column><![CDATA[Thursday]]></column> 
<column><![CDATA[Friday]]></column> 
<column><![CDATA[Saturday]]></column> 
<column><![CDATA[Sunday]]></column></x> 
<row height='224'><![CDATA[01|02|03|04|05|06|07]]></row> 
<row height='224'><![CDATA[08|09|10|11|12|13|14]]></row> 
<row height='224'><![CDATA[15|16|17|18|19|20|21]]></row> 
<row height='310'><![CDATA[22|23|24|25|26|27|28]]></row></scale> 

<event week='4' day='3' type='event_clear' x='300' y='672' width='93.52554744525547'      height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 06:00 Wicked]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='696' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 07:00 Ben Hur Live]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='720' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 07:00 Giselle]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='744' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 08:00 Billy Connolly]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='768' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 09:00 Giselle]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='792' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 10:00 Legally Blonde The Musical1]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='816' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 12:00 Giselle]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='840' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 12:00 Legally Blonde The Musical2]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='864' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 13:00 Legally Blonde The Musical3]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='888' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 14:00 Wicked]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='912' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 16:00 Billy Connolly]]></body></event> 
<event week='4' day='3' type='event_clear' x='300' y='936' width='93.52554744525547' height='13' len='1'><body backgroundColor='' color=''><![CDATA[• 22:00 Jersey Boys]]></body></event></data> 

正如您所看到的,我嘗試使用SimpleXMLElement,但我也會對任何其他解決方案感到滿意。

+0

不知道該鏈接是否提供了我的答案,但我終於明白了。請注意,在我的問題事件沒有ID字段,所以有點棘手。 – Ricardo

+0

是的,這個'id'沒有你需要的答案,但是我明白你的想法確實如此:) – Wrikken

回答

0

那麼這是最後工作給我的片段,當我沒有ID並嘗試刪除前N個節點。

//Where this->xml is my x 
$node = $this->xml->xpath("/data/event"); 
$total_nodes = count($node)- $n_events; 

for($i=0; $i<$total_nodes; $i++){ 
    $dom=dom_import_simplexml($this->xml->event[0]); 
    $dom->parentNode->removeChild($dom); 
} 
//If you want back to String 
//$this->xml->asXML(); //At this point I want a SimpleXML with a content that i wish,