I am attempting to create a script that will search through a directory tree, find any xml files, and update an index (in a MySQL D😎 whenever the script finds a file that hasn't been previously indexed.
My problem is with the XML parse function (something I am not too familar with). Each xml file has a pair of ID tags that contain a unique number. I want the "idCheck" function to return a true/false on whether it finds the number already listed in the DB, but the extra functions involved in parsing have me confused as to how best to do this. How would I get the return value from the "contents" func back to the script that calls "idCheck."
Here is what I have so far.
If you happen to spot any other probs/issues, please speak up.
Thx.
function idCheck($file) {
// function to check if a file's id is already in DB
$xmlparser = xml_parser_create();
xml_set_element_handler($xmlparser, "startTag", "endTag");
xml_set_character_data_handler($xmlparser, "contents");
$current = "";
if (!($fp = fopen($file, "r"))) {
die("failed to open $file");
} else {
while ($data = fread($fp, 4096)){
$data=eregi_replace(">"."[[:space:]]+"."<","><",$data);
if (!xml_parse($xmlparser, $data, feof($fp))) {
$reason = xml_error_string(xml_get_error_code($xmlparser));
$reason = xml_get_current_line_number($xmlparser);
die($reason);
}
}
fclose($fp);
}
xml_parser_free($xmlparser);
}
function startTag($parser, $name, $attribs){
global $current;
$current = $name;
}
function endTag($parser, $name){
global $current;
}
function contents($parser, $data){
global $current;
if ($current=="ID") {
// Look for ID match in DB
mysql_connect(localhost,"xxx","xxx");
@mysql_select_db(masterindex) or die ("Unable to select database");
$query="SELECT * FROM cases WHERE fullid='$data'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
// if statement dependent on successful match
if ($num==0) {
echo "returning (false)";
return ("false");
} else {
$casename=mysql_result($result,0,"casename");
echo "returning (name)";
return ($casename);
}
}
}