Here's my script, FYI.
The XML file is a very simple format, its just very very long.
<?
set_time_limit(0);
$insiderest = false;
$tag = "";
$title = "";
$country = "";
$address = "";
$city = "";
$state = "";
$zip = "";
$phone = "";
$url = "";
// Create an XML parser
$xml_parser = xml_parser_create();
// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement");
// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");
// Open the XML file for reading
$fp = fopen("/path/to/zmlfile.txt","r")
or die("Error opening xml file.");
// Read the XML file one line at a time
while ($data = fgets($fp,4096))
{
//Clean String for XML parser
$data = utf8_encode($data);
$data = str_replace("&","&",$data);
// Parse each line with the XML parser created above
xml_parse($xml_parser, $data, feof($fp))
// Handle errors in parsing
or die("XML error: ".xml_error_string(xml_get_error_code($xml_parser))."(".xml_get_error_code($xml_parser).") at line ".xml_get_current_line_number($xml_parser)."++$data++");
}
// Close the XML file
fclose($fp);
// Free up memory used by the XML parser
xml_parser_free($xml_parser);
function startElement($parser, $tagName, $attrs)
{
global $insiderest, $tag;
if ($insiderest)
{
$tag = $tagName;
}
elseif ($tagName == "RESTAURANT")
{
$insiderest = true;
}
}
function characterData ($parser, $data)
{
global $insiderest, $tag, $title, $country, $address, $city, $state, $zip, $phone, $url;
if ($insiderest)
{
switch ($tag)
{
case "D:TITLE":
$title .= mysql_real_escape_string(trim($data));
break;
case "COUNTRY":
$country .= mysql_real_escape_string(trim($data));
break;
case "ADDRESS":
$address .= mysql_real_escape_string(trim($data));
break;
case "CITY":
$city .= mysql_real_escape_string(trim($data));
break;
case "STATE":
$state .= mysql_real_escape_string(trim($data));
break;
case "ZIP":
$zip .= mysql_real_escape_string(trim($data));
break;
case "PHONE":
$phone .= mysql_real_escape_string(trim($data));
break;
case "URL":
$url .= mysql_real_escape_string(trim($data));
break;
}
}
}
function endElement($parser, $tagName)
{
global $insiderest, $tag, $title, $country, $address, $city, $state, $zip, $phone, $url,$xml_parser;
if ($tagName == "RESTAURANT")
{
$query = "INSERT INTO `chefmoz_list` (name,country,address,city,state,zip,phone,url) VALUES('$title','$country','$address','$city','$state','$zip','$phone','$url')";
mysql_query($query);
echo "Insert restuarant `$title` into database [".mysql_insert_id()."]\n";
$insiderest = false;
$title = "";
$country = "";
$address = "";
$city = "";
$state = "";
$zip = "";
$phone = "";
$url = "";
}
}
?>