
January 10th, 2004, 08:51 AM
|
|
Junior Member
|
|
Join Date: Nov 2003
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
parsing problem, help plz !
Quote:
?php
$currentTag = "";
$values = array();
$allowedFields = array("productname", "brand", "promotionText", "description", "deepLink", "imageURL", "Price");
$xml_file="info.xml";
// my database obviously
$host = "";
$user = "";
$pass = "";
$db = "";
$table = "";
function startElementHandler($parser, $name, $attributes)
{
global $currentTag;
$currentTag = $name;
}
function endElementHandler($parser, $name)
{
global $values, $currentTag;
global $connection, $table;
if (strtolower($name) == "Story")
{
$query = "INSERT INTO $table";
$query .= "(headline, url, pubsource, pubdate, arrival_time, category, expiration_date, text) ";
$query .= "VALUES(\"" . join("\", \"", $values) . "\");";
$result = mysql_query($query) or dies ("Error in query: $query. " . mysql_error());
$values = array();
$currentTag = "";
}
}
function characterDataHandler($parser, $data)
{
global $currentTag, $values, $allowedFields;
$currentTag = strtolower($currentTag);
if (in_array($currentTag, $allowedFields) && trim($data) != "")
{
$values[$currentTag] = mysql_escape_string($data);
}
}
$xml_parser = xml_parser_create();
xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, TRUE);
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, FALSE);
xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
mysql_select_db($db) or die ("Unable to select database!");
if (!($fp = fopen($xml_file, "r")))
{
die("File I/O error: $xml_file");
}
while ($data = fread($fp, 4096))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
{
$error_code = xml_get_error_code($xml_parser);
die("XML parser error (error code " . $error_code . "): " . xml_error_string($error_code) . "<br>Error occurred at line " . xml_get_current_line_number($xml_parser));
}
}
xml_parser_free($xml_parser);
mysql_close($connection);
?> |
I have created this php file to parse a xml file into a mysql database. It doesn't seem to be producing any errors but basically doesn't work. Maybe only a simple problem, any help would be welcome.
|