This is the type of feed which I'm pulling data from...
<item>
<title>This is a title</title>
<description>Description.</description>
<link>Link</link>
<media:content width="60" height="50" url="http://www.someurl.com"/>
<media:content width="150" height="80" url="http://www.someurl.com"/>
</item>
<item>
<title>This is a title</title>
<description>Description.</description>
<link>Link</link>
</item>
<item>
<title>This is a title</title>
<description>Description.</description>
<link>Link</link>
<media:content width="60" height="50" url="http://www.someurl.com"/>
</item>
Here's how I do it...
<?php
$xml="$_GET['url']"; //link to RSS Feed
$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);
for ($i=0; $i<=10; $i++)
{
$x=$xmlDoc->getElementsByTagName('item');
$item_title=$x->item($i)->getElementsByTagName('title')
->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->item($i)->getElementsByTagName('link')
->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->item($i)->getElementsByTagName('description')
->item(0)->childNodes->item(0)->nodeValue;
$x = $xmlDoc->getElementsByTagNameNS("http://search.yahoo.com/mrss/", "content");
$image = $x->item($i)->getAttribute('url');
echo "<img src='$image'/>";
echo ("<p><a href='" . $item_link
. "'>" . $item_title . "</a>");
echo ("<br />");
echo ($item_desc . "</p>");
}
?>
As you can see, some of the child nodes of each <item> element often occur twice, only once or not at all I.E <media:content> in this example.
This means that although the title, description and link will be printed but when there isn't an image under a particular <item> node then it would just print the next image which is supposed to be printed with a different title and description - hope that makes sense.
What I am intending to do is to be able to recognize when there isn't a <media:content> element then just print the title/description/link but not the image because it belongs to the next <item> down. And if there are two, only print one and move on. I really need some help with this as I've become frustrated trying to solve this issue!