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