PHP-XML Demo: XML to HTML

This is a slightly modified example from the XML manual:

Here we go:


"

RECIPE LIST

", "RECIPE" => "
", "RECIPE_NAME" => "

", "AUTHOR" => "Author: ", "MEAL" => "

", "COURSE" => "", "INGREDIENTS" => "

Ingrediants

    ", "ITEM" => "
  1. ", "DIRECTIONS" => "

    Directions

    " ); $end_array = array( "LIST" => "
    ", "RECIPE" => "
    ", "RECIPE_NAME" => "

", "AUTHOR" => "
", "MEAL" => "

", "COURSE" => "", "INGREDIENTS" => "", "ITEM" => "", "DIRECTIONS" => "" ); function startElement($parser, $name, $attrs) { global $begin_array; // print "DEBUG: $name
"; if ($htmlexpr = $begin_array[$name]) { print "$htmlexpr"; } } function endElement($parser, $name) { global $end_array; if ($htmlexpr = $end_array[$name]) { print "$htmlexpr"; } } function characterData($parser, $data) { print $data; } $xml_parser = xml_parser_create(); // use case-folding so we are sure to find the tag in $begin_array // does this REALLY work ??? / DKS 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); ?>
D.K.S.