Sunteți pe pagina 1din 2

XML to SQLite

First part: XML to PHP


SQLite is a full fledged RDBMS. You can mistake it as flat file. It's the sleekest and the fastest
RDBMS under the sun. It is fully comfortable with SQL language.

For working with SQLite , XML and PHP you need to install the SQLite2 and or SQLite-3 and PHP-5
or above and enable DOM/XML extension in PHP.

Fortunately all the above get installed by default once you install database,webserver and PHP from a
typical linux CD (Fedora Core-5 or above, suse 10.1 , mandriva 2008 or any other )

For older linux you have to install sqlite-2 ,php-sqlite and DOM/XML separately.
Sqlite-2 is available from http://www.sqlite.org

With the help of DOM/XML , PHP can decode any length of XML file and then can store easily in
SQLITE database.

In the first part we will read an xml file through a simple htnl uploader and PHP DOM/XML reader.
…................ upload.htm …...........
<html> <body>
<form enctype="multipart/form-data"
action="import.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<table width="600">
<tr> <td>Names file:</td>
<td><input type="file" name="file" /></td>
<td><input type="submit" value="Upload" /></td> </tr>
</table> </form>
</body> </html>

…............... import.php ….................


<?php
$data = array();
function add_person( $first, $second, $third, $fourth,$fifth )
{
global $data;
$data []= array(
'first' => $first,
'second' => $second,
'third' => $third,
'fourth' => $fourth,
'fifth' => $fifth ); // Lets read the first five columns time beeing.
}

if ( $_FILES['file']['tmp_name'] ) {
$dom = DOMDocument::load( $_FILES['file']['tmp_name'] );
$xname = $_FILES['file']['name'];
echo '<br>File Name:'.$xname; // Print file name here.
$rows = $dom->getElementsByTagName( 'Row' );
$first_row = true; // Or you can make it false if it contains headers.

foreach ($rows as $row) {


if ( !$first_row ) { // If it finds some data.
$first = $second = $third = $fourth = $fifth="";
$index = 1;
$cells = $row->getElementsByTagName( 'Cell' );
foreach( $cells as $cell ) {
$ind = $cell->getAttribute( 'Index' );
if ( $ind != null ) $index = $ind;
if ( $index == 1 ) $first = $cell->nodeValue; // Transfer the first column to a variable
if ( $index == 2 ) $second = $cell->nodeValue; // Transfer the second column to another variable
if ( $index == 3 ) $third = $cell->nodeValue;
if ( $index == 4 ) $fourth = $cell->nodeValue;
if ( $index == 5 ) $fifth = $cell->nodeValue;
$index += 1; // Skip to next row.
}
add_person( $first, $second, $third, $fourth,$fifth ); // Add to an array variable
}}}
// Print out the contents of the file in a table
printf('<table border=1');
printf("</td><td>First</td><td>Second</td><td>Third</td><td>Fourth</td><td>fifth</td></tr>");
foreach( $data as $row ) {
$a1=$row['first'];
$a2=$row['second'];
$a3=$row['third'];
$a4=$row['fourth'];
$a5=$row['fifth'];
}
printf("</td><td>$a1</td><td>$a2</td><td>$a3</td><td>$a4</td><td>$a5</td></tr></p>");
}
echo"</table>\n";
?>

You can download the upload.htm and import.php files here.

Once downloaded , put them in the document root directory of your webserver. Supply any xml file and
then see how PHP outputs the first five columns on the browser screen. For testing just take out any
excel file having only one worksheet. Open it in openoffice or in MicroSoft excel an save it as xml file
and the sample xml file is ready.

2nd part: PHP to SQLite

S Bera
Mumbi

S-ar putea să vă placă și