Jump to content

simpleXML Find node by attribute


fagnonk

Recommended Posts

Ok I am experimenting with the dom import xml to delete nodes by finding there attribute:

 

Here is what I have so far:

<?php

$xml = simplexml_load_file('file.xml');

$doc=new SimpleXMLElement($data);
foreach($doc->list as $list)
{
    if($list['name'] == 'kill') {
        $dom=dom_import_simplexml($doc);
        $dom->parentNode->removeChild($dom);
    }
}
echo $doc->asXml();


xml:

[code]
<?xml version="1.0" encoding="utf-16"?>
<!-- http://www.designvillain.com/vp/list1.xml -->
<item>

<list name="kill" videotitle="mytitle" link="mylink">
    <thumb>h1.jpg</thumb>      
</list> 

</item>

 

?>[/code]

I think that I am probably going down the right path here, but its not working and I can't figure out why. Any obvious mistakes?

<?php
$el = new SimpleXmlElement('file.xml'); // $el == item
$sizeOf = sizeof($el->list);
for ($i = 0; $i < $sizeOf; ++$i) {
    $attributes = $el->list[$i]->attributes();
    if ($attributes['name'] === 'kill') {
        unset($el->list[$i]);
    }
}

print $el;
?>

       

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.