Parses an XML file and show its contents on the page. By default is assumes example.xml file in the same direcory as the script is.

”);
break;
default:
//ignore other tags
}
}

function piHandler($parser, $target, $data)
{
if($target == “php”)
{
eval($data);
}
else
{
print(htmlentities($data));
}
}

function defaultHandler($parser, $data)
{
global $defaultText;

$defaultText .= $data;
}

function ndataHandler($parser, $name, $base, $systemID, $publicID,
$notation)
{
print(“\n”);
}

function notationHandler($parser, $name, $base, $systemID, $publicID)
{
print(“\n”);
}

function externalHandler($parser, $name, $base, $systemID, $publicID)
{
//here you could create another parser
print(“\n”);

return(TRUE);
}

/*
** Initialize
*/

//create parser
if(!($parser = xml_parser_create()))
{
print(“Could not create parser!
\n”);
exit();
}

//make sure we’re using ISO-8859-1 encoding
if(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING) !=
“ISO-8859-1”)
{
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING,
“ISO-8859-1”);
}

//register handlers
xml_set_character_data_handler($parser, “cdataHandler”);
xml_set_element_handler($parser, “startHandler”, “endHandler”);
xml_set_processing_instruction_handler($parser, “piHandler”);
xml_set_default_handler($parser, “defaultHandler”);
xml_set_unparsed_entity_decl_handler($parser, “ndataHandler”);
xml_set_notation_decl_handler($parser, “notationHandler”);
xml_set_external_entity_ref_handler($parser, “externalHandler”);

/*
** Parse file
*/
if(!($fp = fopen(“example.xml”, “r”)))
{
print(“Couldn’t open example.xml!
\n”);
xml_parser_free($parser);
exit();
}

while($line = fread($fp, 1024))
{
if(!xml_parse($parser, $line, feof($fp)))
{
//Error, so print full info
print(“ERROR: ” .
xml_error_string(xml_get_error_code($parser)) .
” at line ” .
xml_get_current_line_number($parser) .
“, column ” .
xml_get_current_column_number($parser) .
“, byte ” .
xml_get_current_byte_index($parser) .

\n”);
}
}

//destroy parser
xml_parser_free($parser);

print(“Text handled by the default handler:\n”);
print(“

\n”);
?>