Jump to content

simpleXML Find node by attribute


fagnonk

Recommended Posts

I want to be able to find a node based on an attribute in an XML file. I have been looking at the simpleXML documentation for a while but I can't imagine a way to do this. Any Suggestions? Can anyone point me to an example? I have been googling it for a while to no avail.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

       

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.