use xml_set_character_data_handler outside of scope

From: Date: Mon, 13 Sep 2004 10:42:19 +0000
Subject: use xml_set_character_data_handler outside of scope
Groups: php.general 
Request: Send a blank email to php-general+get-196493@lists.php.net to get a copy of this message
I've tried a couple of different ways to use this function to set a variable outside of the usual scope of my characterData function but I'm not having much luck. Heres some sample code: <?php $xml_repository = "/var/www/html/mets/sample/"; $depth = array(); $searchable_tags = array("MODS:TITLE"); $current_data = ""; $dir_array = array(); $hits = 0; $MAXHITS = 100; $search_term; $flag = false;
                                                                                                                                              
function startElement($parser, $name, $attrs) {
       global $depth, $searchable_tags, $current_data, $hits, $colour, $flag;
                                                                                                                                              
       if (in_array($name,$searchable_tags)) {
               if (preg_match("/".$search_term."/i",$current_data)) {
                       $flag = true;
                       echo "<div bgcolor=\"#$colour\">$current_data</div><br />";
                       $hits++;
               }
       }
       $depth[$parser]++;
}
                                                                                                                                              
function characterData($parser, $data) {
       global $current_data;
       $current_data =  $data;
}
                                                                                                                                              
function endElement($parser, $name) {
       global $depth, $flag;
       $depth[$parser]--;
       if ($flag) { $flag = false; }
} function search_data($term,$data) {
       if (preg_match($term,$data)) { return true; } else { return false; }
}                                                                                                                                              
                                                                                                                                              
// read all the xml files and parse through search engine if ($handle = opendir($xml_repository)) {
       $i = 0;
       while (false !== ($file = readdir($handle))) {
               $dir_array[$i] = $file;
               $i ++;
       }
       closedir($handle);
}
                                                                                                                                              
for ($i = 2; $i < count($dir_array); $i++) {
       $xml_parser = xml_parser_create("UTF-8");
       xml_set_element_handler($xml_parser, "startElement", "endElement");
       xml_set_character_data_handler($xml_parser, "characterData");
                                                                                                                                              
       if (!($fp = fopen($xml_repository.$dir_array[$i], "r"))) {
               die("could not open XML input");
       }
                                                                                                                                              
       while ($data = fread($fp, 4096)) {
               if ($hits >= $MAXHITS) { echo "<p><b>over 100 hits!\n</b> Stopping here.</p>"; exit; }
               if (!xml_parse($xml_parser, $data, feof($fp))) {
                       die(sprintf("XML error: %s at line %d",
                               xml_error_string(xml_get_error_code($xml_parser)),
                               xml_get_current_line_number($xml_parser)));
               }
       }
                                                                                                                                              
       xml_parser_free($xml_parser);
}
                                                                                                                                              
?> If I use $current_data = $data; I get nothing back at all, If I use $current_data .= $data; I get the concatenated response as expected but is not what I am after. All I want is the value of $data at that particular point in the parse. Any help much appreciated. -- Dan Field <dof@llgc.org.uk> Support Programmer: Cymru ar y we / Wales on the Web cy_GB: http://www.cymruarywe.org en_GB: http://www.walesontheweb.org

« previous php.general (#196493) next »