Skip to main content

Load and Insert XML data into MySQL with PHP simplexml function

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 :D
Step 1 : Persiapkan Database


  1. Buat database dengan nama db_tutorial

  2. Siapkan tabel dengan nama tb_dataxml, dengan struktur tabel seperti gambar dibawah ini.

  3. Done!



Step 2 : Persiapkan Folder Kerja


  1. Buat folder dengan nama tutorphp dalam document root anda

  2. Simpan semua file dalam tutorial ini dalam folder tersebut.



Step 3 : Membuat script koneksi ke DB


  1. Ketikkan script berikut,







    1
    2
    3
    4
    5
    6
    7
    8
    9
    <?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());
    ?>



  2. simpan dengan nama connect.php



Step 4 : Menyiapkan data XML


  1. Ketikkan script berikut,







    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?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>



  2. simpan dengan nama record.xml



Step 5 : Membuat script untuk me-load XML, menyimpan ke database, dan menampilkan XML



  1. Ketikkan script berikut,







    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    <?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 year

    echo '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>';
    }
    ?>



  2. simpan dengan nama loadxml.php



Step 6 : Testing Code


  1. Pergi ke http://localhost/tutorphp/loadxml.php. Anda akan melihat data dari record.xml telah diparsing dan ditampilkan seperti gambar dibawah :D

  2. 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 :D 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 :D
SISINDOTEK - I.T Training & Solution Provider
Jl. Sukasenang VI - 6 B Cikutra Bandung
Jawa Barat 40124
Tel. 022-71242266, 0812.8733.1966
Email : info@sisindotek.com , YM. sisindotek
www.sisindotek.com , facebook.com/sisindotek , twitter.com/sisindotek

Comments

Popular posts from this blog

Sewa Ruang Meeting Daerah Citarum Bandung

Sewa ruang meeting kantor, training di Bandung Hubungi 0816-4038-78. Lokasi Ruang Meeting berada di Jl.Pelajar Pejuang 45 No.23B 1. Paket per Jam      - Biaya : Rp.50.000 / org / jam     - Fasilitas : Snack & Drink 2. Paket Half-Day     - Waktu : 09.00 s/d 13.00 Wib     - Biaya : Rp.120.000 / org      - Fasilitas : Makan siang 1x, Snack & Drink 3. Paket Full Day     - Waktu : 09.00 s/d 16.00 Wib     - Biaya : Rp.170.000 / org      - Fasilitas : Makan siang 1x, Coffe break 2x, Snack & Drink 4. Lain-lain     - Sewa Proyektor : Rp.150.000/day     - Extra time : Rp.100.000/Jam Syarat dan Ketentuan : 1. Min. 3org dan Max. 7org 2. Pembayaran H-3 sebelum tgl pelaksanaan 3. Melebihi jam ketentuan dikenakan biaya tambahan Sewa Ruang Meeting Daerah Citarum Bandung Hubungi 0816-4038-78 Ruang meeting murah mulai 50.000 di Buah Batu, Pasteur, Sukajadi, Lengkong, Antapani, Uj...

Agen Milyuner BISTOK Baru : Karya Suhendar, dari Tasikmalaya

Agen Milyuner Baru : Karya Suhendar http://www.bisnistoko.com/myhome72 Karya Suhendar dari Tasikmalaya ( http://www.bisnistoko.com/myhome72 ) telah bergabung menjadi Agen Milyuner BISTOK dengan komisi tak terbatas dan terus bertambah setiap harinya, agen milyuner bistok untuk siapa saja. Berapapun harga iklan yang Anda informasikan komisi anda mencapai Rp 8 juta dalam 2 minggu atau 3,3 Milyar dalam 2 bulan, semua sangat mudah dan Anda tidak bekerja sendirian mesin otomatis kami akan membantu Anda, silahkan bergabung bersama Karya Suhendar di http://www.bisnistoko.com/myhome72 sekarang juga, kini giliran anda !, Oleh : RumahModel Ini baru Toko Online Terpercaya, Daftar sekali untung berkali kali !

How to read .epub ebook

Cara membaca ebook epub, berikut informasi software yang bisa membaca ebook epub sumber : http://epublishersweekly.wordpress.com/about/how-to-read-epub/ Ebooks in the Format: ePub The ebook editions of our paperback books are available in two formats: PDF and ePub. The PDF format is primarily designed for reading on desktop and laptop computers. (But of course, many portable devices can easily read PDF documents.) The new ePub format reads beautifully on iPhones, iPods, and a number of PDAs and ebook readers. And is also possible, and fun, to read the ePub ebooks on your desktop or laptop computer. Read ePub Online BookWorm from O’Reilly Labs. Not only for reading ePub ebooks, BookWorm lets can store your ePub library online. And expect future upgrades to this already-excellent interface. Read ePub on Your iPod or iPhone Stanza from Lexcycle. Everybody loves Stanza, and when you try it you will see why. Read ePub on Your Desktop Computer or Laptop Computer Stanza from Lexcycle. Sta...