<?php
/****************************************************************************************
 * PHP XML SAX functionality
 ****************************************************************************************/

$parser=xml_parser_create();                                            //create a parser

xml_set_element_handler($parser,"elementStart","elementEnd");           //set event handler for element start/end
xml_set_character_data_handler($parser,"elementData");                  //set event handler for element content

$fp=fopen("dinosaur.xml","r");                                          //open xml file

while ($data=fread($fp,4096))                                           //read the file
{
    $success = xml_parse($parser,$data,feof($fp));                      //parse the line 

    if (! $success)
        echo "XML Error: " . xml_error_string(xml_get_error_code($parser))       
           . "  at line: " . xml_get_current_line_number($parser);    
}

xml_parser_free($parser);                                               //release the parser



//Function to use at the start of an element
function elementStart($parser,$element_name,$element_attrs)
{
    echo "$element_name : ";

    foreach ($element_attrs as $attr_name => $attr_value)
        echo "$attr_name=$attr_value, " ;

    echo "<br />";
}

//Function to use at the end of an element
function elementEnd($parser,$element_name)
{
//    echo "<br />";
}

//Function to use when finding character data
function elementData($parser,$data)
{
    echo "$data <br />";
}

?>