Contoh file xml yang akan kita gunakan diambil dari situs microsoft. Ambil file tersebut lalu simpan sebagai contoh.xml. Sebelum bisa membaca file xml, anda harus menginstall library XML di sistem anda di beberapa distro diberi nama php-xml atau php5-xml. Buat script PHP untuk membaca file tersebut seperti dibawah ini
<?php $filexml = simplexml_load_file('contoh.xml'); // extract semua data print_r ($filexml); ?>
Bila dibuka di browser tampilan script diatas seperti gambar dibawah
biar tampilannya mudah dibaca ubah script menjadi
<?php $filexml = simplexml_load_file('contoh.xml'); echo "< pre>"; print_r ($filexml); echo "< /pre>"; ?>
hasil eksekusi script diatas makin memudahkan data untuk dibaca
SimpleXMLElement Object ( [book] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk101 ) [author] => Gambardella, Matthew [title] => XML Developer's Guide [genre] => Computer [price] => 44.95 [publish_date] => 2000-10-01 [description] => An in-depth look at creating applications with XML. ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => bk102 ) [author] => Ralls, Kim [title] => Midnight Rain [genre] => Fantasy [price] => 5.95 [publish_date] => 2000-12-16 [description] => A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. )
sekarang bila kita mau mengambil author, title, genre, price, publish_date dan description ubah scriptnya menjadi
<?php $filexml = simplexml_load_file('contoh.xml'); //echo "<pre>"; ///print_r ($filexml); //echo "
“;
foreach ($filexml as $element => $content) {
echo $content -> author . ‘
‘;
echo $content -> title . ‘
‘;
echo $content -> genre . ‘
‘;
echo $content -> price . ‘
‘;
echo $content -> publish_date . ‘
‘;
echo $content -> description . ‘
‘;
}
?>
Tinggal di kreasikan sesuai dengan kebutuhan anda, atau bisa juga langsung di export ke database MySQL. Untuk source code bisa diambil dibawah