RSS parser med flere kilder
Hvordan får jeg dette script til at kunne vise en liste af nyheder fra flere kilder, gerne sorteret efter dato og tid.<?php
class RSSParser {
var $link = "";
var $title = "";
var $pubdate = "";
var $description = "";
var $inside_item = false;
var $current_tag = "";
var $array_items = array();
function startElement($parser, $name, $attrs='')
{
$this->current_tag = $name;
if ($this->current_tag == "ITEM")
{
$this->inside_item = true;
}
}
function endElement($parser, $tagName, $attrs='')
{
if ($tagName == "ITEM")
{
$this->array_items[] = array($this->title, $this->link, $this->description, $this->pubdate);
$this->link = "";
$this->title = "";
$this->description = "";
$this->pubdate = "";
$this->inside_item = false;
}
}
function characterData($parser, $data)
{
if ($this->inside_item)
{
switch($this->current_tag)
{
case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;
case "PUBDATE":
$this->pubdate .= $data;
break;
default:
break;
}
}
}
function parse_results($xml_parser, $rss_parser, $file)
{
xml_set_object($xml_parser, &$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler( $xml_parser, "characterData");
$xml_data = @file_get_contents($file);
if ($xml_data)
{
xml_parse($xml_parser, $xml_data);
}
xml_parser_free($xml_parser);
}
} // end class
$xml_parser = xml_parser_create();
$rss_parser = new RSSParser();
$rss_parser->parse_results($xml_parser, &$rss_parser, "http://sporten.tv2.dk/rss/sporten.xml");
$rss_array = $rss_parser->array_items;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>RSS Sporten</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
if (is_array($rss_array))
{
foreach($rss_array as $array)
{
list($title, $link, $description, $pubdate) = $array;
// Her kommer det så ud...
echo "$title, $link, $description, $pubdate";
}
}
?>
</body>
</html>