Page 1 of 1

Parse xml and send datas to MySQL database

Posted: Tue May 18, 2010 3:06 pm
by timberpro
Hi for everybody who reading this post, i`m new in php and need some help. I want to write a small script to parse xml file (from file) and send datas to table. Here is the prototype of code, but i need more help to finishing.
Here is the .html (xml) document online: http://www.ika.timberpro.net/test/fajlok/001b.html

Here is the code:

Code: Select all

<?php

/**
 * @author Gabika
 * @copyright 2010
 */


if ($handle = opendir('fajlok/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $file_handle = fopen("fajlok/".$file, "r");
$file_contents = fread($file_handle, filesize('fajlok/'.$file));

$array[$i]=explode("<li class=\"name\">"."<span class=\"textLabel\">"."Név: </span>", $file_contents);


for($j=0; $j<count($array[$i]); $j++) {
$temp=explode("</li>", $array[$i][$j]);
$array[$i][$j]=$temp[0];
}



fclose($file_handle);
$array[$i][0]="";
$i++;
        }
    }
    closedir($handle);
}


for($k=0; $k<count($array); $k++){
for($l=1; $l<count($array[$k]); $l++){
echo "$l ".$array[$k][$l]."<br/>";

}
}

if ($handle = opendir('fajlok/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $file_handle = fopen("fajlok/".$file, "r");
$file_contents = fread($file_handle, filesize('fajlok/'.$file));

$array[$i]=explode("<li class=\"citylevel\">"."<span class=\"textLabel\">"."Város mérete: </span>", $file_contents);


for($j=0; $j<count($array[$i]); $j++) {
$temp=explode("</li>", $array[$i][$j]);
$array[$i][$j]=$temp[0];
}



fclose($file_handle);
$array[$i][0]="";
$i++;
        }
    }
    closedir($handle);
}

for($k=0; $k<count($array); $k++){
for($l=1; $l<count($array[$k]); $l++){
echo "$l ".$array[$k][$l]."<br/>";

}
}

if ($handle = opendir('fajlok/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $file_handle = fopen("fajlok/".$file, "r");
$file_contents = fread($file_handle, filesize('fajlok/'.$file));

$array[$i]=explode("<li class=\"owner\">"."<span class=\"textLabel\">"."Játékos: </span>", $file_contents);


for($j=0; $j<count($array[$i]); $j++) {
$temp=explode("</li>", $array[$i][$j]);
$array[$i][$j]=$temp[0];
}



fclose($file_handle);
$array[$i][0]="";
$i++;
        }
    }
    closedir($handle);
}

for($k=0; $k<count($array); $k++){
for($l=1; $l<count($array[$k]); $l++){
echo "$l ".$array[$k][$l]."<br/>";

}
}

if ($handle = opendir('fajlok/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $file_handle = fopen("fajlok/".$file, "r");
$file_contents = fread($file_handle, filesize('fajlok/'.$file));

$array[$i]=explode("<li class=\"name\">"."<span class=\"textLabel\">"."Pontok: </span>", $file_contents);


for($j=0; $j<count($array[$i]); $j++) {
$temp=explode("</li>", $array[$i][$j]);
$array[$i][$j]=$temp[0];
}



fclose($file_handle);
$array[$i][0]="";
$i++;
        }
    }
    closedir($handle);
}

for($k=0; $k<count($array); $k++){
for($l=1; $l<count($array[$k]); $l++){
echo "$l ".$array[$k][$l]."<br/>";

}
}

if ($handle = opendir('fajlok/')) {
$i=0;
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            $file_handle = fopen("fajlok/".$file, "r");
$file_contents = fread($file_handle, filesize('fajlok/'.$file));

$array[$i]=explode("<li class=\"ally\">"."<span class=\"textLabel\">"."Szövetség: </span>", $file_contents);


for($j=0; $j<count($array[$i]); $j++) {
$temp=explode("</li>", $array[$i][$j]);
$array[$i][$j]=$temp[0];
}



fclose($file_handle);
$array[$i][0]="";
$i++;
        }
    }
    closedir($handle);
}

for($k=0; $k<count($array); $k++){
for($l=1; $l<count($array[$k]); $l++){
echo "$l ".$array[$k][$l]."<br/>";

}
}
?>
But is not complete and very large and no connection to the database.
I want to use a separate file for storing connect details.

Best Regards,
Gabor

Re: Parse xml and send datas to MySQL database

Posted: Tue May 18, 2010 8:16 pm
by Jonah Bron
The link you gave is an HTML file, so there will likely be parse errors. You must also realize that xml to database is not exactly a linear conversion. A database table is not hierarchical, while xml is. That means you have to orchestrate multiple tables to create a hierarchical structure. Here's a couple of good links.

http://www.databaseanswers.org/tutorial ... /index.htm
http://www.google.com/search?q=database ... e+tutorial

If you're not trying to get all of the data, and just a certain linear part of it, please indicate which.