parsing RSS (how to parse DC:DATE)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
erbsland
Forum Newbie
Posts: 1
Joined: Thu Mar 25, 2004 6:54 am
Contact:

parsing RSS (how to parse DC:DATE)

Post by erbsland »

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:

Code: Select all

<?php

	$m = 1;
	$newz = array();

	class draque
	&#123;
		// globale Variablen anlegen
		var $insideitem = false;
		var $tag = "";
		var $title = "";
		var $description = "";
		var $link = "";
		var $pubdate = "";

		function startElement($parser, $tagName, $attrs)
		&#123;
			// Schauen, obman sich im ITEM tag befindet
			if ($this->insideitem)
			&#123;
				$this->tag = $tagName;
			&#125;
				elseif ($tagName == "ITEM")
			&#123;
				$this->insideitem = true;
			&#125;
		&#125;

		function endElement($parser, $tagName)
		&#123;
			if($tagName == "ITEM")
			&#123;
				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++;
			&#125;
		&#125;

		function characterData($parser, $data)
		&#123;
			if ($this->insideitem)
			&#123;
				switch ($this->tag)
				&#123;
					case "PUBDATE":
					$this->pubdate .= $data;
					break;

					case "TITLE":
					$this->title .= $data;
					break;

					case "DESCRIPTION":
					$this->description .= $data;
					break;

					case "LINK":
					$this->link .= $data;
					break;
				&#125;
			&#125;
		&#125;
	&#125;

	$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++)
	&#123;
		switch($wo)
		&#123;
			case 0: $counter=$newz&#1111;$i]; break;
			case 1: $datum=$newz&#1111;$i]; break;
			case 2: $link=$newz&#1111;$i]; break;
			case 3: $descr=$newz&#1111;$i]; break;
		&#125;
		$wo++;
		if( $wo > 3 ) //Wenn $wo 4 ist sind alle Daten des aktuellen Datensatzes gesetzt
		&#123;
			$pieces = explode( " - ", $descr );

			// Datum wird umformatiert
			$splitdate = explode (" ", $datum);
			$newdate = $splitdate&#1111;1] . ".&nbsp;" . $splitdate&#1111;2] . ".&nbsp;" . $splitdate&#1111;3] . ": ";

			if( $counter < 6 )
			&#123;
			  if( strlen( $descr ) > 45 )
        &#123;
          $dots = "...";
        &#125; else &#123;
          $dots = "";
        &#125;
        $cutted = substr( htmlspecialchars( stripslashes( $descr ) ) , 0, 45 );

			  printf ("\n&#9642;&nbsp;<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&#9642;&nbsp;$newdate<a href="$link">$descr</a><br>";
			&#125;

			$wo = 0;
		&#125;
	&#125;

	unset($i);

?>
Any help would be highly appreciated! :D

dominik
Post Reply