| 1 | <?php |
| 2 | $filename="http://www.nanodot.org/nanodot.rss"; |
| 3 | class RSSParser { |
| 4 | |
| 5 | var $insideitem = false; |
| 6 | var $tag = ""; |
| 7 | var $title = ""; |
| 8 | var $description = ""; |
| 9 | var $link = ""; |
| 10 | |
| 11 | function startElement($parser, $tagName, $attrs) { |
| 12 | if ($this->insideitem) { |
| 13 | $this->tag = $tagName; |
| 14 | } elseif ($tagName == "ITEM") { |
| 15 | $this->insideitem = true; |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | function endElement($parser, $tagName) { |
| 20 | if ($tagName == "ITEM") { |
| 21 | printf("<dt><b><a href='%s'>%s</a></b></dt>", |
| 22 | |
| 23 | trim($this->link),htmlspecialchars(trim($this->title))); |
| 24 | |
| 25 | printf("<dd>%s</dd>",htmlspecialchars(trim($this->description))); |
| 26 | $this->title = ""; |
| 27 | $this->description = ""; |
| 28 | $this->link = ""; |
| 29 | $this->insideitem = false; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | function characterData($parser, $data) { |
| 34 | if ($this->insideitem) { |
| 35 | switch ($this->tag) { |
| 36 | case "TITLE": |
| 37 | $this->title .= $data; |
| 38 | break; |
| 39 | case "DESCRIPTION": |
| 40 | $this->description .= $data; |
| 41 | break; |
| 42 | case "LINK": |
| 43 | $this->link .= $data; |
| 44 | break; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | $xml_parser = xml_parser_create(); |
| 51 | $rss_parser = new RSSParser(); |
| 52 | xml_set_object($xml_parser,&$rss_parser); |
| 53 | xml_set_element_handler($xml_parser, "startElement", "endElement"); |
| 54 | xml_set_character_data_handler($xml_parser, "characterData"); |
| 55 | $fp = fopen($filename,"r") |
| 56 | or die("Error reading RSS data."); |
| 57 | while ($data = fread($fp, 4096)) |
| 58 | xml_parse($xml_parser, $data, feof($fp)) |
| 59 | or die(sprintf("XML error: %s at line %d", |
| 60 | xml_error_string(xml_get_error_code($xml_parser)), |
| 61 | xml_get_current_line_number($xml_parser))); |
| 62 | fclose($fp); |
| 63 | xml_parser_free($xml_parser); |
| 64 | |
| 65 | ?> |