Jump to content

[SOLVED] ximple XML question


mendoz

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/116026-solved-ximple-xml-question/
Share on other sites

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.