All pastes #47838 Raw Edit

Untitled

public text v1 · immutable
#47838 ·published 2006-04-01 22:58 UTC
rendered paste body
<?
// RSS NEWS READER
// Simple RSS Reader to grab news from whichever site you choose from a select box
// I've included a few popular RSS News Feeds, but add your own using the listbox
// Easy As.
?>

</p>
<form name="form1" method="post" action="<?php echo $PHP_SELF ?>">
  <select name="backend" id="backend">
    <option value="http://www.citytv.com/toronto/news.xml">Pulse 24</option>
    <option value="http://news.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss091.xml">BBC NEWS</option>
    <option value="http://rss.cnn.com/rss/cnn_world.rss">CNN WORLD NEWS</option>
    <option value="http://www.microsite.reuters.com/rss/topNews">REUTERS NEWS</option>
  </select>
  <input type="submit" name="Submit" value="Submit">
</form>
<p>&nbsp;</p>
<?php
// Variables Needed (some may not be but oh well)
$backend = $_POST['backend'];
$insideitem = false;
$tag = "";
$title = "";
$description = "";
$link = "";
// end

function startElement($parser, $tagName, $attrs) {

  // The function used when an element is encountered

  global $insideitem, $tag;

  if ($insideitem) {

    $tag = $tagName;

  } elseif ($tagName == "ITEM") {

    $insideitem = true;
  }

}

function characterData($parser, $data) {

  // The function used to parse all other data than tags

  global $insideitem, $tag, $title, $description, $link;
  
  if ($insideitem) {
  
    switch ($tag) {
    case "TITLE":
      $title .= $data;
      break;
    case "DESCRIPTION":
      $description .= $data;
      break;
    case "LINK":
      $link .= $data;
      break;
    }

  }

}

function endElement($parser, $tagName) {

  // This function is used when an end-tag is encountered.

  global $insideitem, $tag, $title, $description, $link;
  
  if ($tagName == "ITEM") {
   echo "<table width=\"100%\" border=\"0\"><tr>
    <td class=\"main-table\">
	<span class=\"nav-head\"><strong>
	";
  printf("<p><b><a href='%s'>%s</a></b></p>", // make our title into an actual link
       trim($link),htmlspecialchars(trim($title))); // remove html characters from the title
  
  echo "</tr>";
	 echo "<tr>
    <td class=\"nav\">";
	printf("<p>%s</p>",$description); // Print out the live journal entry
   	echo"</td>";
 echo "</tr>";
  echo "</table>";
	echo "<br>";
	$title = $description = $link = $insideitem = false;

   }

}

// Now to the parsing itself. Starts by creating it:

$xml_parser = xml_parser_create();

// Then setup the handlers:

xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

// Open the actual datafile:

$fp = fopen($backend, r);

// Run through it line by line and parse:

while ($data = fread($fp, 4096)) {
  xml_parse($xml_parser, $data, feof($fp))
    or die(sprintf("XML error: %s at line %d",
           xml_error_string(xml_get_error_code($xml_parser)),
           xml_get_current_line_number($xml_parser)));
}

// Close the datafile

fclose($fp);

// Free any memmory used

xml_parser_free($xml_parser);

?>