Reference: PHP enthusiast This example in this example is located in http://www.slashdot.org/slashdot.xml This PHP file read, the XML file content is below the content of the PHP file, and the PHP file and Slashdot.xml is placed in the same folder php $ open_tags = array ('story' => '
$ close_tags = array ('story' => ' story>', 'title' => ' title>', 'URL', 'Author' => ' author' > ');?>?>
PHP // The following is the definition function to extract the data:
// Processing Start Tag Attribute Independence // $ Attrs is a multi-dimensional array, the key value is the value of the value of this property Function StartElement ($ PARSER, $ Name, $ attrs = ') {global $ open_tags, $ { Temp, $ current_tag; $ current_tag = $ name; if ($ format = $ open_tags [$ name]) {switch ($ name) {case 'story': echo 'new story:'; break; default: Break;} }
// $ current_tag tells us that the tag that is being processed, then we will then use // // when you encounter story> tags in the CharacterData function, we know all of the temporary variables to Flush next Mark Function endElement ($ Parser $ Name, $ attrs = '') {Global $ close_tags, $ TEMP, $ CURRENT_TAG; IF ($ format = $ close_tags [$ name]) {switch ($ name) {case 'story': return_page ($ TEMP) $ TEMP = ''; Break; default: Break;}}}
// Transfer to this function data ///, for example,
PHP Function Return_page () {Global $ TEMP; Echo 'O '. $ TEMP ['Title']. '
'; Echo' Author: '. $ TEMP [' Author '].'
'; echo' ------------------------- ---- '; Echo'
';} // Analysis content $ XML_FILE =' slashdot.xml '; // Define the character set, default is UTF-8 $ type =' UTF-8 '; // parser The $ xml_parser = xml_parser_create ($ type); // set the resolution options xml_parser_set_option ($ xml_parser, XML_OPTION_CASE_FOLDING, true); xml_parser_set_option ($ xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); // tell PHP to be invoked when the element found What function // These functions also handle the elementality of the element XML_set_element_handler ($ xml_parser, 'startelent ",' endelement '); // telling PHP what function XML_SET_CHARACTER_DATA_HANDAL ($ XML_PARSER,' CharacterData '); if (!); If (! $ fp = fopen ($ XML_FILE, 'R')))) {DIE ("Unable to open the $ xml_file file for parsing! N");} // By loop to resolve the entire file While ($ data = fread ($ fp, 4096 )) {If ($ data))) {echo 'error'. "N";} if (! Xml_parse ($ xml_parser, $ data, feof ($ fp))) {Die (Sprintf) ("XML Error:% S at line% DNN", XML_ERROR_STRI Ng (XML_GET_ERROR_CODE ($ XML_PARSER)), XML_GET_CURRENT_LINE_NUMBER ($ XML_PARSER)));}} XML_Parser_Free ($ XML_PARSER);?>>>