Parsing XML the Sequel :)

From: Date: Sat, 10 Feb 2001 18:37:32 +0000
Subject: Parsing XML the Sequel :)
Groups: php.general 
Request: Send a blank email to php-general+get-39090@lists.php.net to get a copy of this message
The page I want to parse is: http://213.224.136.110:8080/test.xml And this is my code: <?php //Define Opening Tags of XML $open_tags = array( 'STORY' => '<nasdaqamex-dot-com>', 'STOCK_NAME' => '<issue-name>', 'LAST_SALE' => '<last-sale-price>', 'PREVIOUS_CLOSE' => '<previous-close-price>', 'NET_CHANGE_PCT' => '<net-change-pct>', 'STOCK_URL' => '<issuer-web-site-url>'); //Define Closing Tags of XML $close_tags = array( 'STORY' => '</nasdaqamex-dot-com>', 'STOCK_NAME' => '</issue-name>', 'LAST_SALE' => '</last-sale-price>', 'PREVIOUS_CLOSE' => '</previous-close-price>', 'NET_CHANGE_PCT' => '</net-change-pct>', 'STOCK_URL' => '</issuer-web-site-url>'); // 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 'STORY': echo 'New Story: '; 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 </STORY> 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]){ switch($name){ case 'STORY': return_page($temp); $temp = ''; break; default: break; } } } // this function is passed data between elements // theu $data would equal 'Title Here' // in the line <TITLE>Title Here</TITLE> function characterData($parser, $data){ global $current_tag, $temp, $catID; switch($current_tag){ case 'STOCK_NAME': $temp['stock_name'] = $data; $current_tag = ''; break; case 'LAST_SALE': $temp['last_sale'] = $data; $current_tag = ''; break; default: break; } } function return_page() { global $temp; echo 'o <A HREF="'.$temp['stock_name'].'">'.$temp['last_sale'].'</A><BR>'; } // what are we parsing? $xml_file = 'test.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); //Just to see if it has done something echo 'yahoo'; ?>

« previous php.general (#39090) next »