Hi anyone,
I am trying to set up an app. to read an XML file into an array and then insert this into a mysql table row by row.
So far I have an array that prints screen only and an mysql insert that inserts blank rows.
The array is:
<?php // reads the XML file and prints to screen
class Ergebnis {
function Ergebnis ($e) { foreach ($e as $k=>$v) $this->$k = $e[$k]; } }
function read($filename) {
$data = implode ("",file($filename));
$parser = xml_parser_create ();
xml_parser_set_option ($parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option ($parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct ($parser,$data,$values,$tags);
xml_parser_free ($parser); echo $data;
foreach ($tags as $key=>$val) { if ($key == "teilnehmer")
{$ranges = $val; for ($i=0; $i < count($ranges); $i+=2)
{ $offset = $ranges[$i] + 1;
$len = $ranges[$i + 1] - offset;
$tdb[ ] = parse(array_slice($values, $offset, $len)); } }
else { continue; } } return $tdb;
}
function parse($values)
{for ($i=0; $i < count($values); $i++)
$erg [ $values [$i] ["tag"] ] = $values[$i] ["value"];
return new Ergebnis($erg);}
$xml = read("contact2.xml");
echo "** Array objects:\n";
print_r($xml);
The Mysql_query (which I cannot configure correctly to run with the Ergebnis Class) goes:
$insert="INSERT INTO teilnehmer (cod,name,Stnr,Rang,verein,lizenz)
VALUES ('$data[0]','$array[1]','$array[2]','$array[3]','$array[4]','$array[5]')"
or die(mysql_error("Invalid Query :"));
mysql_query($insert);
printf ("Records inserted: %d\n", mysql_affected_rows());
If someone can throw some light on how to retrieve the elements from the XML parser row by row and insert to the table I would be very much obliged!