Re: METS/XML Search facility

From: Date: Mon, 27 Sep 2004 14:30:10 +0000
Subject: Re: METS/XML Search facility
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-197957@lists.php.net to get a copy of this message
Dan Field wrote:
Burhan Khalid wrote:
Dan Field wrote:
I'm trying to write an XML Document search engine specifically for METS encoded data ( http://www.loc.gov/standards/*mets*/ ). I've tried knocking together some proof of concept code in PHP4 using both DOM and xml_parser but I've run into dificulties with both. Can anyone point me at some code/class which enables me to search within an XML document for the existance of $string within a set list of tags eg. <METS:title>Some Title Here</METS:title> <METS:description>This is a description of this document</METS:description> It seems like a very straightforward task.
You can do searching from the function that is designated as the character handler (if you are using the xml_* functions). You would just have to keep track of what tag you are checking by setting flags, etc.
This is what I tried first off, but I found that I was getting forced breaks within the data for some reason. I believe certain characters caused the parser to stop and parse in multiple passes. I'm probably not explaining it very well, heres the code I was using: $xml_repository = "/var/www/html/mets/sample/"; $searchable_tags = array("MODS:TITLE"); $current_data = ""; $dir_array = array(); $hits = 0; $MAXHITS = 100; $search_term = "William"; $validtag = false; $found = false;
                                                                                                                                              [ trim ]
       xml_parser_set_option ($xml_parser,$XML_OPTION_SKIP_WHITE ,1);
Needs to be XML_OPTION_SKIP_WHITE, no $
       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",
Here you need to make sure you trim your input to the parser. I find that most problems result with extra whitespace. Try these suggestions to see if it helps.

« previous php.general (#197957) next »