AJAX RSS Reader

Example Explained – The HTML Page

function showRSS(str)
{
if (str.length==0)
  {
  document.getElementById("rssOutput").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("rssOutput").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getrss.php?q="+str,true);
xmlhttp.send();
}
<form>

Select an RSS-feed:
Google News
MSNBC News

</form>
<br>
<div id="rssOutput">RSS-feed will be listed here...</div>

The PHP File
The page on the server called by the JavaScript above is a PHP file called “getrss.php”:

load($xml);

//get elements from “”
$channel=$xmlDoc->getElementsByTagName(‘channel’)->item(0);
$channel_title = $channel->getElementsByTagName(‘title’)
->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName(‘link’)
->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName(‘description’)
->item(0)->childNodes->item(0)->nodeValue;

//output elements from “”
echo(”

” . $channel_title . ““);
echo(”
“);
echo($channel_desc . ”

“);

//get and output “” elements
$x=$xmlDoc->getElementsByTagName(‘item’);
for ($i=0; $iitem($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;

echo (”

” . $item_title . ““);
echo (”
“);
echo ($item_desc . ”

");
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *