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

PRIKOM, Privat Kursus Komputer di Cimincrang Gedebage Bandung

PRIKOM - Les Privat Komputer di wilayah sekitar Cimincrang Gedebage Bandung, Whatsapp  0812-8733-1966, PRIKOM menyediakan jasa Privat dan kursus komputer terbaik dan murah di bandung dengan topik unggulan  Word, Excel, Powerpoint, Access database, Excel Vba Macro, MS Project, Corel, Photoshop dan lainnya , Guru dapat dipanggil ke rumah Anda sekitar Gedebage dengan standard protokol kesehatan covid-19, atau bisa datang ke kantor PRIKOM , hubungi Admin PRIKOM 081287331966 (Whatsapp) atau visit https://privatkomputerbandung.wordpress.com , Jam operasional 09.00-21.00, privat komputer untuk seluruh wilayah bandung dan cimahi , Untuk level Pemula sampai dengan Level Mahir, Peserta akan didampingi tenaga pengajar (Pria/ Wanita) yang berpengalaman dan sabar dalam menyampaikan materi privat / kursus komputer #privatkomputerbandung #bandung #kursuskomputer #tutorialkomputer #komputerpemula #belajarkomputer #ilmukomputer #jakarta #bekasi  #Gedebage #Cimincrang Jasa Privat Komputer guru Ke rumah...

Training MariaDB di Jakarta Bandung Bali Batam;

MariaDB ; di Purnama Academy adalah Tempat Pelatihan dan Training MariaDB   di Jakarta Bandung Bali Batam;   Download silabus, Informasi jadwal training seminar workshop di kota jakarta, bandung, bogor, semarang, yogyakarta, surabaya, bali, lombok, timor leste, jayapura, ambon, manado, makasar, palu, banjarmasin, palangkaraya, bontang, samarinda, pontianak, batam, bangka, lampung, banten, palembang, medan, batam, jambi, padang, aceh , Pendaftaran, Silabus   , jadwal atau   Informasi lebih lanjut hubungi Tim sales Ph atau whatsapp 0838-0838-0001, atau visit purnamaAcademy.com , Tersedia paket Public Training, Exclusive Group Training atau inhouse training dengan biaya terjangkau.   TRAINING TOPIK 2019 LAINNYA hubungi   PURNAMAACADEMY.COM atau 0838-0838-0001 Training ITIL v.3/2011 Foundation (IT Infrastructure Library) di Jakarta Bandung Bali Batam; Training COBIT 5.0 (Control Objectives for Information and Related Technology)   di Jakarta Bandung Bali Bat...

SEO , Top Keyword Research 2015

Google's Hummingbird update created a lot of anxiety, but ultimately, it could be a good thing for the industry, because it frees us from the tyranny of competing for a limited number of top keywords. Essentially, the role of the Hummingbird algorithm is to better answer those longer-tail queries users are typing in Google. If your pages are optimized for these more conversational queries, you have a better chance of top rankings. Try a new, niche-based approach to keywords, which allows you to double or even triple the list of profitable keywords in your SEO arsenal. This article explains the four steps for doing keyword research the modern way, using SEO PowerSuite or other tools. 1. Ideas: Most search marketers simply think of the main keywords related to their businesses, plop them into a tool like Google Keyword Planner, and then run with the keyword list it delivers. However, search habits vary widely: Searchers may use hundreds of different word combinations to describe the ...