Artur Filipiak
Artur Filipiak

Reputation: 9157

PHP, DOM - remove first several nodes

The xml file looks like this:

<newVotes>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_524700002.jpg" id="1"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5317gg.jpg" id="2"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5393.jpg" id="3"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_2299.jpg" id="4"/>    
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4977.jpg" id="5"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4977BW.jpg" id="6"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_4914.jpg" id="7"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="flowergirl.jpg" id="8"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_5393.jpg" id="9"/>
    <image details="14.10.11" path="/test/content/pictures3/" image_name="PIC_2299.jpg" id="10"/>
</newVotes>

I don't know how many "image" nodes will contain my xml, and how many of them goes to remove (it's dynamic amount variable comes from flash module) My question is how to delete for example first 5 "image" nodes in xml, using PHP?

Especialy added "id" nodes because I thought I can delete them using Xpath somehow, but with no lucky...

Thanks for any help. Artur.

Upvotes: 0

Views: 237

Answers (1)

Artur Filipiak
Artur Filipiak

Reputation: 9157

Done... It was pretty simple:

$filesLimit = $_POST['_limit'];
$rateElement = $news_dom->getElementsByTagName('image');
$numberOfFiles = $rateElement->length;
$m = $numberOfFiles - $filesLimit + 1;
if($numberOfFiles>=$filesLimit){         
    for ($i = 0; $i < $m; $i++) {
        $nodesToRemove = $rateElement->item(0)->parentNode->removeChild($rateElement->item(0));   
    } 
}

Upvotes: 1

Related Questions