There are two basic types of XML Parser:
Tree-based Parser: This parser converts an XML document into a tree structure. It analyzes the entire document and provides APIs to access tree elements, such as the Document Object Model (DOM ).
Event-based Parser: treats XML documents as a series of events. When a specific event occurs, the parser calls a function to process it.
The Expat parser is an event-based parser.
The XML Expat parser is a core component of PHP. You can use these functions without installation.
XML file:
George
John
Reminder
Don't forget the meeting!
Initialize the XML Parser:
"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEADING": echo "Heading: "; break; case "BODY": echo "Message: "; } }//Function to use at the end of an elementfunction stop($parser,$element_name) { echo "
"; }//Function to use when finding character datafunction char($parser,$data) { echo $data; }//Specify element handlerxml_set_element_handler($parser,"start","stop");//Specify data handlerxml_set_character_data_handler($parser,"char");//Open XML file$fp=fopen("test.xml","r");//Read datawhile ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); }//Free the XML parserxml_parser_free($parser);?>