0

我需要删除字符串中 XML 的前 N ​​个节点。

    $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,但我也会对任何其他解决方案感到满意。

4

1 回答 1

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,
于 2013-10-13T21:30:17.850 回答