Studi Kasus : Mengambil data dari XML dan menyimpannya ke dalam database (mysql)
Kebutuhan : Webserver Packages, already installed.
OKAY, no need for trivial chit-chat. Let’s just practice 😀
Step 1 : Persiapkan Database
- Buat database dengan nama db_tutorial
- Siapkan tabel dengan nama tb_dataxml, dengan struktur tabel seperti gambar dibawah ini.
- Done!
Step 2 : Persiapkan Folder Kerja
- Buat folder dengan nama tutorphp dalam document root anda
- Simpan semua file dalam tutorial ini dalam folder tersebut.
Step 3 : Membuat script koneksi ke DB
- Ketikkan script berikut,
[sourcecode language=”php”]
<?php
$host = "localhost";
$user = "root"; //adjust according to your mysql setting
$pass = ""; //adjust according to your mysql setting
$dbName = "db_tutorial";
mysql_connect($host, $user, $pass);
mysql_select_db($dbName)
or die ("Connect Failed !! : ".mysql_error());
?>
[/sourcecode] - simpan dengan nama connect.php
Step 4 : Menyiapkan data XML
- Ketikkan script berikut,
[sourcecode language=”xml”]
<?xml version="1.0" encoding="iso-8859-1"?>
<data>
<record>
<title>Time to Go</title>
<author>June Sushan</author>
<publisher>Gramedia Inc</publisher>
<date>
<month>June</month>
<year>1989</year>
</date>
</record><record>
<title>Time to Come</title>
<author>Alan Smart</author>
<publisher>Elexmedia Inc</publisher>
<date>
<month>April</month>
<year>1989</year>
</date>
</record></data>
[/sourcecode]
- simpan dengan nama record.xml
Step 5 : Membuat script untuk me-load XML, menyimpan ke database, dan menampilkan XML
- Ketikkan script berikut,
[sourcecode language=”php”]
<?php
include ‘connect.php’;
if( !$xml = simplexml_load_file(‘record.xml’) ) //using simplexml_load_file function to load xml file
{
echo ‘load XML failed ! ‘;
}
else
{
echo ‘<h1>This is the Data</h1>’;
foreach( $xml as $record ) //parse the xml file into object
{
$title = $record->title; //get the childnode title
$author = $record->author; //get the child node author
$publisher = $record->publisher; //get the child node publisher
$month = $record->date->month; //get the child node month
$year = $record->date->year; //get the child node yearecho ‘Title : ‘.$title.'<br />’;
echo ‘Author : ‘.$author.'<br />’;
echo ‘Publisher : ‘.$publisher.'<br />’;
echo ‘Month : ‘.$month.'<br />’;
echo ‘Year : ‘.$year.'<br />’;
echo ‘<br>’;//save to database
$q = "INSERT INTO tb_dataxml VALUES(”,’$title’,’$author’,’$publisher’,’$month $year’)";
$result = mysql_query($q);
}
if ($result) {
echo ‘<h2>Success Save to Database </h2>’;
}
else echo ‘<h2>Failed Save to Databaase</h2>’;
}
?> [/sourcecode] - simpan dengan nama loadxml.php
Step 6 : Testing Code
- Pergi ke http://localhost/tutorphp/loadxml.php. Anda akan melihat data dari record.xml telah diparsing dan ditampilkan seperti gambar dibawah 😀
- Cek database anda, pastikan bahwa data dari XML telah tersimpan dalam tabel yang telah anda siapkan pada step 1
So, this is time to trivial chit-chat Ha Ha 😀 XML adalah extensible Markup Language.
Why XML? XML adalah format data standar (yang telah disepakati) yang digunakan dalam pertukaran data. Karena ke-global-an XML itulah, XML sering digunakan dalam komunikasi data karena formatnya yang platform independent 😀