XML 锟斤拷签映锟斤拷锟斤拷锟斤拷

Example #1 锟斤拷 XML 映锟斤拷为 HTML

锟斤拷锟斤拷锟斤拷直锟接地斤拷 XML 锟斤拷签映锟斤拷为 HTML 锟斤拷签锟斤拷 锟斤拷"map_array"锟斤拷未锟揭碉拷锟斤拷元锟截斤拷锟斤拷锟斤拷锟皆★拷 锟斤拷然锟斤拷锟斤拷锟斤拷锟斤拷只锟斤拷锟斤拷囟锟斤拷锟� XML 锟侥碉拷锟斤拷锟斤拷锟斤拷锟斤拷锟矫★拷

<?php
$file 
"data.xml";
$map_array = array(
    
"BOLD"     => "B",
    
"EMPHASIS" => "I",
    
"LITERAL"  => "TT"
);

function 
startElement($parser$name$attrs
{
    global 
$map_array;
    if (isset(
$map_array[$name])) {
        echo 
"<$map_array[$name]>";
    }
}

function 
endElement($parser$name
{
    global 
$map_array;
    if (isset(
$map_array[$name])) {
        echo 
"</$map_array[$name]>";
    }
}

function 
characterData($parser$data
{
    echo 
$data;
}

$xml_parser xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parserXML_OPTION_CASE_FOLDINGtrue);
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($fp4096)) {
    if (!
xml_parse($xml_parser$datafeof($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);
?>