fagnonk Posted July 10, 2009 Share Posted July 10, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/165448-simplexml-find-node-by-attribute/ Share on other sites More sharing options...
fagnonk Posted July 10, 2009 Author Share Posted July 10, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/165448-simplexml-find-node-by-attribute/#findComment-872623 Share on other sites More sharing options...
ignace Posted July 10, 2009 Share Posted July 10, 2009 <?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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/165448-simplexml-find-node-by-attribute/#findComment-872627 Share on other sites More sharing options...
fagnonk Posted July 10, 2009 Author Share Posted July 10, 2009 Thanks for looking at it, still no look though. I should point out that list has a child node, could this make a difference? Quote Link to comment https://forums.phpfreaks.com/topic/165448-simplexml-find-node-by-attribute/#findComment-872630 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.