<?php

// sample XML parser for simple student record
//
// This approach won't work on any XML document that contains
// many different FIRST tags or LAST tags...

// Assumes that the parser does case folding 
//  (converts all tag names to uppercase).
//  this can be turned off with xml_parser_set_option...


$file = "example.xml";

// this function is called whenever the parser finds a start tag.
// the tag name and attributes are passed in.
function startElement($parser, $name, $attrs) {
    global $curtag;
	$curtag = $name;
}

// this function is called whenever the parser finds an end tag.
// the tag name is passed in.
// All we do is clear our global variable to indicate we
//   are no in the middle of a tag anymore.
function endElement($parser, $name) {
    global $curtag;
	$curtag = '';
}


// Whever the parser finds some character data (the text inbetween
// a start tag and end tag) it calls this function.
// we check the global $curtag to see if this is cdata we care about,
//  and if so print it out.
function charData($parser,$cdata) {
	global $curtag;
	if ($curtag == "FIRST") 
  		echo "Name is $cdata ";
	else if ($curtag == "LAST")
		echo "$cdata \n";
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser,"charData");

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);
?>