'', 'ECONOMY' => '', 'POLITICALFREEDOM' => '', 'POPULATION' => '', 'TAX' => ''); $close_tags = array( 'CIVILRIGHTS' => '', 'ECONOMY' => '', 'POLITICALFREEDOM' => '', 'POPULATION' => '', 'TAX' => ''); // handles the attributes for opening tags // $attrs is a multidimensional array keyed by attribute // name and having the value of that attribute function startElement($parser, $name, $attrs=''){ global $open_tags, $temp, $current_tag; $current_tag = $name; if ($format = $open_tags[$name]){ switch($name){ case 'CIVILRIGHTS': print "
  • Civil Rights: "; break; case 'ECONOMY': print "
  • Economy: "; break; case 'POLITICALFREEDOM': print "
  • Political Freedom: "; break; case 'POPULATION': print "
  • Population: "; break; case 'TAX': print "
  • Tax: "; break; default: break; } } } // $current_tag lets us know what tag we are currently // dealing with - we use that later in the characterData // function. // // when we see a closing tag we know that it is time to // flush our temp variables and prepare to move onto // the next one function endElement($parser, $name, $attrs=''){ global $close_tags, $temp, $current_tag; if ($format = $close_tags[$name]){ #this doesn't make any sense, just return the shiz and clear it out return_page($temp . "
  • "); $temp = ''; /* switch($name){ case 'CIVILRIGHTS': return_page($temp . ""); $temp = ''; break; default: break; } */ } } // this function is passed data between elements // theu $data would equal 'Title Here' // in the line Title Here function characterData($parser, $data){ global $current_tag, $temp, $catID; switch($current_tag){ case 'CIVILRIGHTS': $temp['civilrights'] = $data . ""; $current_tag = ''; break; case 'ECONOMY': $temp['economy'] = $data . ""; $current_tag = ''; break; case 'POLITICALFREEDOM': $temp['politicalfreedom'] = $data . ""; $current_tag = ''; break; case 'POPULATION': $temp['population'] = $data . " million" . ""; $current_tag = ''; break; case 'TAX': $temp['tax'] = $data . ""; $current_tag = ''; break; default: break; } } function return_page(){ global $temp; print $temp['civilrights']; print $temp['economy']; print $temp['politicalfreedom']; print $temp['population']; print $temp['tax']; } // what are we parsing? $xml_file = 'ns.xml'; // declare the character set - UTF-8 is the default $type = 'UTF-8'; // create our parser $xml_parser = xml_parser_create($type); // set some parser options xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true); xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8'); // this tells PHP what functions to call when it finds an element // these funcitons also handle the element's attributes xml_set_element_handler($xml_parser, 'startElement','endElement'); // this tells PHP what function to use on the character data xml_set_character_data_handler($xml_parser, 'characterData'); if (!($fp = fopen($xml_file, 'r'))) { die("Could not open $xml_file for parsing!\n"); } // loop through the file and parse baby! while ($data = fread($fp, 4096)) { if (!($data = utf8_encode($data))) { echo 'ERROR'."\n"; } if (!xml_parse($xml_parser, $data, feof($fp))) { die(sprintf( "XML error: %s at line %d\n\n", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser))); } } xml_parser_free($xml_parser); ?>