1 |
<?php |
2 |
|
3 |
// XML file with news data |
4 |
$file = "news.xml"; |
5 |
// show all news entries by default |
6 |
$max_items = -1; |
7 |
|
8 |
$current_tag = array(); |
9 |
$current_tag_body_is_empty = true; |
10 |
$current_link_ref = ""; |
11 |
|
12 |
function startElement($parser, $name, $attribs) { |
13 |
global $max_items; |
14 |
global $current_tag; |
15 |
global $current_tag_body_is_empty; |
16 |
global $current_link_ref; |
17 |
|
18 |
if ($max_items == 0) return; |
19 |
|
20 |
array_push($current_tag, $name); |
21 |
|
22 |
$current_tag_body_is_empty = true; |
23 |
|
24 |
if ($name == "entry") { |
25 |
if (count($attribs) && isset($attribs["date"])) { |
26 |
$d = $attribs["date"]; |
27 |
// pack into a (single cell) table so (flaoting) images wont vertically overlap out of the news block border |
28 |
echo "<div class=\"news\"><table><tr><td>"; |
29 |
echo "<span class=\"news_date\">$d</span> "; |
30 |
} else echo "<b>???</b> "; |
31 |
} else if ($name == "link") { |
32 |
$current_link_ref = (isset($attribs["ref"])) ? $attribs["ref"] : ""; |
33 |
echo "<a href=\"$current_link_ref\">"; |
34 |
if (count($attribs) && isset($attribs["img"])) { |
35 |
$pic = $attribs["img"]; |
36 |
echo "<img src=\"$pic\" style=\"float: left; margin: 4px;\">"; |
37 |
$current_tag_body_is_empty = false; |
38 |
} |
39 |
} else if ($name == "list") { |
40 |
echo "<ul class=\"news\">"; |
41 |
} else if ($name == "li") { |
42 |
echo "<li class=\"news\">"; |
43 |
} |
44 |
} |
45 |
|
46 |
function endElement($parser, $name) { |
47 |
global $max_items; |
48 |
global $current_tag; |
49 |
global $current_tag_body_is_empty; |
50 |
global $current_link_ref; |
51 |
|
52 |
if ($max_items == 0) return; |
53 |
|
54 |
array_pop($current_tag); |
55 |
|
56 |
if ($name == "entry") { |
57 |
echo "</td></tr></table></div>\n"; |
58 |
if ($max_items > 0) $max_items--; |
59 |
} |
60 |
else if ($name == "link") { |
61 |
if ($current_tag_body_is_empty) echo $current_link_ref; |
62 |
echo "</a>"; |
63 |
$current_link_ref = ""; // reset |
64 |
} |
65 |
else if ($name == "list") echo "</ul>"; |
66 |
else if ($name == "li") echo "</li>"; |
67 |
} |
68 |
|
69 |
function characterData($parser, $data) { |
70 |
global $max_items; |
71 |
global $current_tag; |
72 |
global $current_tag_body_is_empty; |
73 |
|
74 |
$current_tag_body_is_empty = false; |
75 |
|
76 |
if ($max_items == 0) return; |
77 |
|
78 |
echo $data; |
79 |
} |
80 |
|
81 |
if (isset($HTTP_GET_VARS['max_items'])) { |
82 |
$max_items = $HTTP_GET_VARS['max_items']; |
83 |
} |
84 |
|
85 |
$xml_parser = xml_parser_create(); |
86 |
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
87 |
xml_set_element_handler($xml_parser, "startElement", "endElement"); |
88 |
xml_set_character_data_handler($xml_parser, "characterData"); |
89 |
if (!($fp = fopen($file, "r"))) { |
90 |
die("Could not open news XML file!"); |
91 |
} |
92 |
while ($data = fread($fp, 4096)) { |
93 |
if (!xml_parse($xml_parser, $data, feof($fp))) { |
94 |
die(sprintf("XML error: %s at line %d", |
95 |
xml_error_string(xml_get_error_code($xml_parser)), |
96 |
xml_get_current_line_number($xml_parser))); |
97 |
} |
98 |
} |
99 |
xml_parser_free($xml_parser); |
100 |
|
101 |
?> |