Code has been added to clipboard!

Understanding PHP RSS Parser

Example
<?php
  //get q parameter from the URL
  $q = $_GET["q"];

  //check which feed was selected
  if($q == 'reuters_news') {
    $xml_src=("http://news.google.com/news?ned=us&topic=h&output=rss");
  } elseif($q == 'bbc_news') {
    $xml_src=("http://feeds.bbci.co.uk/news/world/rss.xml");
  }

  $xml_doc = new DOMDocument();
  $xml_doc->load($xml_src);

  //get elements from inside the "<channel>"
  $c=$xml_doc->getElementsByTagName('channel')->item(0);
  $c_title = $c->getElementsByTagName('title')
  ->item(0)->childNodes->item(0)->nodeValue;
  $c_link = $c->getElementsByTagName('link')
  ->item(0)->childNodes->item(0)->nodeValue;
  $c_desc = $c->getElementsByTagName('description')
  ->item(0)->childNodes->item(0)->nodeValue;

  //output elements from "<channel>"
  echo("<p><a href='" . $channel_link
    . "'>" . $channel_title . "</a>");
  echo("<br>");
  echo($channel_desc . "</p>");

  //get and output "<item>" elements
  $x = $xmlDoc->getElementsByTagName('item');
  for ($i=0; $i< = 2; $i++) {
    $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;
    echo ("<p><a href='" . $item_link
    . "'>" . $it_title . "</a>");
    echo ("<br>");
    echo ($item_desc . "</p>");
  }
?>