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 |
echo "<div class=\"news\">"; |
28 |
echo "<span class=\"news_date\">$d</span> "; |
29 |
} else echo "<b>???</b> "; |
30 |
} else if ($name == "link") { |
31 |
$current_link_ref = (isset($attribs["ref"])) ? $attribs["ref"] : ""; |
32 |
echo "<a href=\"$current_link_ref\">"; |
33 |
} else if ($name == "list") { |
34 |
echo "<ul class=\"news\">"; |
35 |
} else if ($name == "li") { |
36 |
echo "<li class=\"news\">"; |
37 |
} |
38 |
} |
39 |
|
40 |
function endElement($parser, $name) { |
41 |
global $max_items; |
42 |
global $current_tag; |
43 |
global $current_tag_body_is_empty; |
44 |
global $current_link_ref; |
45 |
|
46 |
if ($max_items == 0) return; |
47 |
|
48 |
array_pop($current_tag); |
49 |
|
50 |
if ($name == "entry") { echo "</div>\n"; |
51 |
if ($max_items > 0) $max_items--; |
52 |
} |
53 |
else if ($name == "link") { |
54 |
if ($current_tag_body_is_empty) echo $current_link_ref; |
55 |
echo "</a>"; |
56 |
$current_link_ref = ""; // reset |
57 |
} |
58 |
else if ($name == "list") echo "</ul>"; |
59 |
else if ($name == "li") echo "</li>"; |
60 |
} |
61 |
|
62 |
function characterData($parser, $data) { |
63 |
global $max_items; |
64 |
global $current_tag; |
65 |
global $current_tag_body_is_empty; |
66 |
|
67 |
$current_tag_body_is_empty = false; |
68 |
|
69 |
if ($max_items == 0) return; |
70 |
|
71 |
echo $data; |
72 |
} |
73 |
|
74 |
if (isset($HTTP_GET_VARS['max_items'])) { |
75 |
$max_items = $HTTP_GET_VARS['max_items']; |
76 |
} |
77 |
|
78 |
$xml_parser = xml_parser_create(); |
79 |
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, false); |
80 |
xml_set_element_handler($xml_parser, "startElement", "endElement"); |
81 |
xml_set_character_data_handler($xml_parser, "characterData"); |
82 |
if (!($fp = fopen($file, "r"))) { |
83 |
die("Could not open news XML file!"); |
84 |
} |
85 |
while ($data = fread($fp, 4096)) { |
86 |
if (!xml_parse($xml_parser, $data, feof($fp))) { |
87 |
die(sprintf("XML error: %s at line %d", |
88 |
xml_error_string(xml_get_error_code($xml_parser)), |
89 |
xml_get_current_line_number($xml_parser))); |
90 |
} |
91 |
} |
92 |
xml_parser_free($xml_parser); |
93 |
|
94 |
?> |