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