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

Jokowi

Jokowi Trending Topic KUMPULAN BERITA JOKOWI AHOK : Celetukan-celetukan Ahok untuk pendatang ... www.merdeka.com/tag/j/ jokowi -ahok Salinan Follow tag merdeka.com akan membantu untuk mendapatkan berita yang sesuai preferensi Anda. Misal Anda suka berita Jokowi Ahok, masukkan email dan Anda hanya akan ... quoted by www.sisindotek.com / www.sindonusa.com Joko Widodo - Wikipedia, the free encyclopedia en.wikipedia.org/wiki/Joko_Widodo Salinan Jokowi will serve as Governor of Jakarta until 20 October 2014, the official commencement date of his term as the 7th President of Indonesia. Contents. quoted by www.sisindotek.com / www.sindonusa.com merdeka.com: Halaman Tag www.merdeka.com/tag/j/ jokowi Salinan Follow tag merdeka.com akan membantu untuk mendapatkan berita yang sesuai preferensi Anda. Misal Anda suka berita Jokowi , masukkan email dan Anda hanya akan ...quoted by www.sisindotek.com / www.sindonusa.com Jokowi www. jokowi .id Salinan Jokowi Bertemu Relawan Di Rumah Tra...

How to Pass ISACA CGEIT Certification Exam Dumps Practice Test

ISACA CGEIT certification is mainly targeted to those candidates who want to build their future in IT Governance domain. CGEIT exam can provide those within an information technology related governance field with great benefits, both with potential employers, and unlock a network of like-minded individuals. ISACA's Certified in the Governance of Enterprise IT (CGEIT) exam certification is framework agnostic and the only IT governance certification for the individual. CGEIT can put you in the role of a trusted advisor to your enterprise. CGEITs maintain an adequate level of current knowledge and proficiency in the field of information systems audit, control and security. The technical skill requirement of CGEIT is that the candidate has relevant professional work experience supporting organizational enterprise information technology. CGEIT is a vendor-neutral enterprise IT governance certification which can help take your career to new heights. Free Exam Practice Question and Answer...

Pijat Urut Bekam Panggilan di Jambi

Beranda terapi bekam Pijat Urut Bekam Panggilan di Jambi Pijat Urut Bekam Panggilan di Jambi APRIL 07, 2020 Terapi Bekam Urut Panggilan Jambi Klinik kesehatan di Jambi Alamat: Jl. Prabu Siliwangi No.226, RT.14, Kasang Jaya, Kec. Jambi Tim., Kota Jambi, Jambi 36141 Jam buka Layanan : Rabu 08.00–22.00 Kamis 08.00–22.00 Jumat 08.00–22.00 Sabtu 08.00–22.00 Minggu 08.00–22.00 Senin 08.00–22.00 Selasa 08.00–22.00 Telepon/Whatsapp :  0852-6666-5854 Provinsi: Jambi TERAPI BEKAM DAN URUT TRADISIONAL ASSALAM Menerima panggilan kerumah dan hotel untuk membantu mengatasi keluhan anda... Apa keluhan anda ? .. apakah ... 1. Sering sakit kepala, darah tinggi 2. Sering sakit leher 3. Tengkuk terasa berat 4. Pundak, bahu terasa sakit 5. Punggung sekitar urat belikat terasa sakit, pegal 6. Tulang punggung terasa sakit, pegal 7. Pinggang dan pinggul terasa sakit, pegal 8. Paha, betis terasa pegal dan sering kesemutan. 9. Tangan terasa tak bertenaga dan mudah capek, 10. Tangan sering keram dan kese...