parsing RSS (how to parse DC:DATE)
Posted: Thu Mar 25, 2004 6:54 am
hi guys
I want to parse the RSS feed from this page:
http://games.slashdot.org/games.rss
All works fine but the date is written in the tag <DC:DATE> and not <PUBDATE> how I know it...
so does anybody know how I can parse <DC:DATE>?
here is the code I am working with:
Any help would be highly appreciated! 
dominik
I want to parse the RSS feed from this page:
http://games.slashdot.org/games.rss
All works fine but the date is written in the tag <DC:DATE> and not <PUBDATE> how I know it...
so does anybody know how I can parse <DC:DATE>?
here is the code I am working with:
Code: Select all
<?php
$m = 1;
$newz = array();
class draque
{
// globale Variablen anlegen
var $insideitem = false;
var $tag = "";
var $title = "";
var $description = "";
var $link = "";
var $pubdate = "";
function startElement($parser, $tagName, $attrs)
{
// Schauen, obman sich im ITEM tag befindet
if ($this->insideitem)
{
$this->tag = $tagName;
}
elseif ($tagName == "ITEM")
{
$this->insideitem = true;
}
}
function endElement($parser, $tagName)
{
if($tagName == "ITEM")
{
global $newz;
global $m;
/*
printf("\n<li>".trim($this->pubdate).": <a href='%s'>%s</a>$i</li>",
trim($this->link),htmlspecialchars(trim($this->title)));
*/
array_push( $newz, $m,$this->pubdate, $this->link, $this->title );
$this->title = "";
$this->description = "";
$this->link = "";
$this->insideitem = false;
$this->pubdate = "";
$m++;
}
}
function characterData($parser, $data)
{
if ($this->insideitem)
{
switch ($this->tag)
{
case "PUBDATE":
$this->pubdate .= $data;
break;
case "TITLE":
$this->title .= $data;
break;
case "DESCRIPTION":
$this->description .= $data;
break;
case "LINK":
$this->link .= $data;
break;
}
}
}
}
$xml_parser = xml_parser_create();
$rss_parser = new draque();
xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
$fp = fopen("http://games.slashdot.org/games.rss","r")
or die("Error reading RSS data.");
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)));
fclose($fp);
xml_parser_free($xml_parser);
// Hier wird das array in 4er aufgeteilt, damit man es brauchbar auslesen kann
$cnt = count( $newz );
$wo = 0;
for( $i=0; $i < $cnt; $i++)
{
switch($wo)
{
case 0: $counter=$newzї$i]; break;
case 1: $datum=$newzї$i]; break;
case 2: $link=$newzї$i]; break;
case 3: $descr=$newzї$i]; break;
}
$wo++;
if( $wo > 3 ) //Wenn $wo 4 ist sind alle Daten des aktuellen Datensatzes gesetzt
{
$pieces = explode( " - ", $descr );
// Datum wird umformatiert
$splitdate = explode (" ", $datum);
$newdate = $splitdateї1] . ". " . $splitdateї2] . ". " . $splitdateї3] . ": ";
if( $counter < 6 )
{
if( strlen( $descr ) > 45 )
{
$dots = "...";
} else {
$dots = "";
}
$cutted = substr( htmlspecialchars( stripslashes( $descr ) ) , 0, 45 );
printf ("\n▪ <a href="javascript:void(2);"
onclick="window.open('%s','myWindow','width=700,height=700,resizable=yes,left=25,top=25,scrollbars=yes');">%s</a><br>",
trim($link),$cutted . $dots);
// print "\n▪ $newdate<a href="$link">$descr</a><br>";
}
$wo = 0;
}
}
unset($i);
?>dominik