PHP, DOM - удалить первые несколько узлов - PullRequest
0 голосов
/ 14 октября 2011

XML-файл выглядит следующим образом:

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

Я не знаю, сколько узлов "image" будет содержать мой xml, и сколько из них будет удалено (это переменная динамического количества, полученная изflash module) Мой вопрос: как удалить, например, первые 5 «изображений» узлов в xml, используя PHP?

Особенно добавили «id» узлы, потому что я думал, что могу как-то удалить их, используя Xpath, но без везения...

Спасибо за любую помощь.Артур.

1 Ответ

1 голос
/ 15 октября 2011

Готово ... Это было довольно просто:

$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));   
    } 
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...