mendoz Posted July 22, 2008 Share Posted July 22, 2008 Hey freaks, I'm trying to parse an XML file in PHP, but I need to do thing simpler. This is my code: <?php $file = "http://godojo.org/new/file.xml"; $data = file_get_contents($file); $xml = new SimpleXMLElement($data); $banners = array(); foreach ($xml->children() as $banner) { // get the banner's ID $banner_id = intval($banner['id']); // Loop through all the banners foreach ($banner as $content) { // start building the link $link = "<a "; // get the attributes for the anchor foreach ($content->a->attributes() as $name=>$val) $link .= $name .'="'.$val.'" '; $link .= "><img "; // get the attributes for the img foreach ($content->a->img->attributes() as $name=>$val) $link .= $name .'="'.$val.'" '; $link .= "/></a>"; } $banners[$banner_id] = $link; } print_r($banners); ?> which outputs: Array ( [1] => <a target="_blank" href="http://www.rubyfortune.com/index.asp?s=wgs16875&a=wgsaffad18294" ><img border="0" alt="gambling" src="http://www.wagershare.com/affiliate_media/Banners/b3627.gif" /></a> [2] => <a target="_blank" href="site2.com" ><img src="2.jpg" /></a> [3] => <a target="_blank" href="site3.com" ><img src="3.jpg" /></a> ) This works, but I don't want to parse every attribute, I just to get everything that is inside the <content> element. For example: <banner id="3"> <content> <a target="_blank" href="site3.com"><img src="3.jpg"/></a> </content> </banner> I just want to get: <a target="_blank" href="site3.com"><img src="3.jpg"/></a> Without parsing each and every one of the attributes, or maybe run out of luck if there is no 'img' element inside the anchor. So how do I "bulk" get all that's inside the <content> element? thanks, mendoz Quote Link to comment https://forums.phpfreaks.com/topic/116026-solved-ximple-xml-question/ Share on other sites More sharing options...
waynew Posted July 22, 2008 Share Posted July 22, 2008 What are you trying to do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/116026-solved-ximple-xml-question/#findComment-596615 Share on other sites More sharing options...
mendoz Posted July 22, 2008 Author Share Posted July 22, 2008 Got it, My problem was that the code for the banner was treated like XML, not a string. Changing '>' to '<' and '<' to '>' solved the problem. Quote Link to comment https://forums.phpfreaks.com/topic/116026-solved-ximple-xml-question/#findComment-596628 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.