Jump to content

PHP RSS Parser missing nodes?


raydowe

Recommended Posts

I need a php expert.

 

I am more of a flash coders so I'm having problems here. I am trying to convert an rss feed (muskokarunning.blogspot.com/atom.xml) to html with php. For some reason that I can't comprehend, the first 3 posts, or 'item' tags are completely ignored! I have tried about 3 'premade' rss parsers, and then wrote my own from scratch, and it's the same thing every time!  >:( I have it working in flash fine, so i'm not sure whats going on. Can anyone notice something wrong with my code or with the tags?

 

Thanks in advance for your help.

 


function blogparse($file)
{
  $doc = new DOMDocument();
  $doc->load( $file );

  $items = $doc->getElementsByTagName( "item" );

$i = 0;
foreach( $items as $item)
{
	$title = $item->getElementsByTagName( "title" );
	$titles[$i] = $title->item(0)->nodeValue;

	$description = $item->getElementsByTagName( "description" );
	$descriptions[$i] = $description->item(0)->nodeValue;

	$link = $item->getElementsByTagName( "link" );
	$links[$i] = $link->item(0)->nodeValue;

	$i++;
};

$finalstring = '';

for ($a=0; $a<count($titles); $a++)
{
                $finalstring .= '<a href="'.$links[$a].'">';
	$finalstring .= "<h1>".$titles[$a]."</h1></a>";
	$finalstring .= "<p>".$descriptions[$a]."</p>";
};

  return($finalstring);

};
  
$parser = blogparse('http://muskokarunning.blogspot.com/atom.xml');
echo($parser);


 

 

Link to comment
https://forums.phpfreaks.com/topic/124062-php-rss-parser-missing-nodes/
Share on other sites

Just use SimpleXML instead of DOM.

 

Thanks for your comment.

 

I took your advice and made another version of the script with simpleXML, but it's doing the SAME THING! I have no idea why this is happening, and would be forever grateful if anyone could give me some insight, or manage to get it working somehow. I feel like I have tried everything at this point, and I'm all out of ideas...

 

Here is my latest attempt

<?php
function blogparse($file)
{
if(!$xml=simplexml_load_file('http://muskokarunning.blogspot.com/atom.xml'))
{
	trigger_error('Error reading XML file',E_USER_ERROR);
}

$finalstring = '';

$channel = $xml->channel;

$items = $channel->item;

foreach($items as $item)
{	
	$name = $item->getName();

	if($name = 'item')
	{
		$finalstring .= '<a href="'. $item->link . '">';
		$finalstring .= '<h1>' . $item->title . '</h1></a>';
		$finalstring .= '<p>' . $item->description . '</p>';
	}
}
  return($finalstring);
}

$parseresult = blogparse('http://muskokarunning.blogspot.com/atom.xml');
echo($parseresult);

?>

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.