13. februar 2004 - 17:52
#1
Arv.
Fuck it. Det er php som er fatsvag.
<?php
$currentTag = "";
$fields = array();
$values = array();
$xml_file="data.xml";
function startElementHandler($parser, $name, $attributes)
{
global $currentTag, $table;
$currentTag = $name;
if (strtolower($currentTag) == "table")
{
$table = $attributes["name"];
}
}
function endElementHandler($parser, $name)
{
global $fields, $values, $count, $currentTag;
global $connection, $table;
if (strtolower($name) == "record")
{
$query = "INSERT INTO $table";
$query .= "(" . join(", ", $fields) . ")";
$query .= " VALUES(\"" . join("\", \"", $values) . "\");";
echo "$query\n";
$fields = array();
$values = array();
$count = 0;
$currentTag = "";
}
}
function characterDataHandler($parser, $data)
{
global $fields, $values, $currentTag, $count;
if (trim($data) != "")
{
$fields[$count] = $currentTag;
$values[$count] = mysql_escape_string($data);
$count++;
}
}
$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");
if (!($fp = fopen($xml_file, "rb")))
{
die("File I/O error: $xml_file");
}
while ($data = fread($fp, 2))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
{
$ec = xml_get_error_code($xml_parser);
die("XML parser error (error code " . $ec . "): " . xml_error_string($ec) .
"<br>Error occurred at line " . xml_get_current_line_number($xml_parser));
}
}
xml_parser_free($xml_parser);
?>
<?xml version="1.0"?>
<table name="readings">
<record>
<a>56565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656</a>
<b>12565656565656565656565656565656565656565656565656565656565656565622</b>
<c>785656565656565656565656565656565656565656565656565656565656565656.5</c>
</record>
<record>
<x>456565656565656565656565656565656565656565656565656565656565656565</x>
<y>-565656565656565656565656565656565656565656565656565656565656565610</y>
</record>
<record>
<x>156565656565656565656565656565656565656565656565656565656565656562</x>
<b>105656565656565656565656565656565656565656565656565656565656565656459</b>
<a>7565656565656565656565656565656565656565656565656565656565656565656</a>
<y>95656565656565656565656565656565656565656565656565656565656565656</y>
</record>
</table>
Outputtet bliver:
INSERT INTO readings(a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c) VALUES("56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "12", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "22", "78", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", ".5"); INSERT INTO readings(x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y) VALUES("4", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "5", "-", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "10"); INSERT INTO readings(x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, x, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, b, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y, y) VALUES("1", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "2", "1", "05", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "64", "59", "75", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "65", "6", "9", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56", "56");
Og hvis man sætter at den læser flere bytes per gang:
INSERT INTO readings(a, b, c) VALUES("56565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656565656", "12565656565656565656565656565656565656565656565656565656565656565622", "785656565656565656565656565656565656565656565656565656565656565656.5"); INSERT INTO readings(x, y) VALUES("456565656565656565656565656565656565656565656565656565656565656565", "-565656565656565656565656565656565656565656565656565656565656565610"); INSERT INTO readings(x, b, a, y) VALUES("156565656565656565656565656565656565656565656565656565656565656562", "105656565656565656565656565656565656565656565656565656565656565656459", "7565656565656565656565656565656565656565656565656565656565656565656", "95656565656565656565656565656565656565656565656565656565656565656");
Det er sgu lidt fucked up.
Det jeg har tænkt mig nu er at splitte mine 500 filer i små filer og så læse dem ind på en gang.