<?PHP
$file = "data.XML";
$map_array = array(
"BOLD" => "B",
"EMPHASIS" => "I",
"LITERAL" => "TT"
);
function startElement($parser, $name, $attrs) {
global $map_array;
if ($Htmltag == $map_array[$name]) {
print "<$Htmltag>";
}
}
function endElement($parser, $name) {
global $map_array;
if ($Htmltag == $map_array[$name]) {
print "</$Htmltag>";
}
}
function characterData($parser, $data) {
print $data;
}
$xml_parser = XML_parser_create();
// 使用大小寫折疊來保證我們能在元素數組中找到這些元素名稱
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($XML_parser, "startElement", "endElement");
xml_set_character_data_handler($XML_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
dIE("could not open XML input");
}
while ($data = fread($fp, 4096)) {
if (!xml_parse($XML_parser, $data, feof($fp))) {
dIE(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($XML_parser)),
xml_get_current_line_number($XML_parser)));
}
}
xml_parser_free($XML_parser);
?>