0

I need remove a the first N nodes of a XML inside of a String.

    $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]);
        }
    }

But that code does not work on this xml inside of the String.

<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>

As you see, I'm trying with SimpleXMLElement, but I will happy with any other solution too.

Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • Not sure about if that link provides my answer, but I finally get it. Please note that in my problem the event has not ID field so is a bit tricky. – Ricardo Oct 13 '13 at 21:22
  • Yeah, the `id` there is not the meat of the answer you need, but I see you figured that out indeed :) – Wrikken Oct 14 '13 at 10:13

1 Answers1

0

Well this is the snippet that finally works to me when I dont have ID and try to delete the first N nodes.

//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,
Ricardo
  • 7,921
  • 14
  • 64
  • 111