Contoh file XML yang akan kita olah, beri nama kurs.xml
. File tersebut bisa disimpen di file hosting atau dimanapun untuk testing. Banyak file XML yang jauh lebih kompleks, tapi untuk belajar ada baiknya dari yang mudah terlebih dahulu agar dapat logikanya.
<?xml version="1.0" encoding="ISO-8859-1"?> <rates> <rate> <matauang>AUD</matauang> <beli> 9,276.00</beli> <jual> 9,757.00</jual> </rate> <rate> <matauang>GBP</matauang> <beli>17,033.00</beli> <jual>17,581.00</jual> </rate> <rate> <matauang>CAD</matauang> <beli>10,343.00</beli> <jual>10,879.00</jual> </rate> <rate> <matauang>CNY</matauang> <beli>1,790.43</beli> <jual>2,148.52</jual> </rate> <rate> <matauang>EUR</matauang> <beli>15.420.00</beli> <jual>16,068.00</jual> </rate> <rate> <matauang>HKD</matauang> <beli>1,663.62</beli> <jual>1,839.98</jual> </rate> <rate> <matauang>JPY</matauang> <beli>130.76</beli> <jual>137.89</jual> </rate> <rate> <matauang>NZD</matauang> <beli>8,699.00</beli> <jual>9,183.00</jual> </rate> <rate> <matauang>SGD</matauang> <beli> 9,966.00</beli> <jual>10,429.00</jual> </rate> <rate> <matauang>SEK</matauang> <beli>1,439.00</beli> <jual>1,497.00</jual> </rate> <rate> <matauang>CHF</matauang> <beli>14,149.00</beli> <jual>14,709.00</jual> </rate> <rate> <matauang>USD</matauang> <beli>13,982.00</beli> <jual>14,474.00</jual> </rate> </rates>
Daripada kita memecah baris perbaris tersebut berdasarkan tag contoh parsing-xml.php
<?php // parsing-xml.php $xmlurl = file_get_contents("http://localhost:9900/kurs.xml"); $xml = simplexml_load_string($xmlurl, "SimpleXMLElement", LIBXML_NOCDATA); var_dump($xml);
jalankan script diatas, struktrurnya lebih mudah dipahami
class SimpleXMLElement#1 (1) { public $rate => array(12) { [0] => class SimpleXMLElement#2 (3) { public $matauang => string(3) "AUD" public $beli => string(9) " 9,276.00" public $jual => string(9) " 9,757.00" } [1] => class SimpleXMLElement#3 (3) { public $matauang => string(3) "GBP" public $beli => string(9) "17,033.00" public $jual => string(9) "17,581.00" } [2] => class SimpleXMLElement#4 (3) { public $matauang => string(3) "CAD" public $beli => string(9) "10,343.00" public $jual => string(9) "10,879.00" } [3] => class SimpleXMLElement#5 (3) { public $matauang => string(3) "CNY" public $beli => string(8) "1,790.43" public $jual => string(8) "2,148.52" } [4] => class SimpleXMLElement#6 (3) { public $matauang => string(3) "EUR" public $beli => string(9) "15.420.00" public $jual => string(9) "16,068.00" } [5] => class SimpleXMLElement#7 (3) { public $matauang => string(3) "HKD" public $beli => string(8) "1,663.62" public $jual => string(8) "1,839.98" } [6] => class SimpleXMLElement#8 (3) { public $matauang => string(3) "JPY" public $beli => string(6) "130.76" public $jual => string(6) "137.89" } [7] => class SimpleXMLElement#9 (3) { public $matauang => string(3) "NZD" public $beli => string(8) "8,699.00" public $jual => string(8) "9,183.00" } [8] => class SimpleXMLElement#10 (3) { public $matauang => string(3) "SGD" public $beli => string(9) " 9,966.00" public $jual => string(9) "10,429.00" } [9] => class SimpleXMLElement#11 (3) { public $matauang => string(3) "SEK" public $beli => string(8) "1,439.00" public $jual => string(8) "1,497.00" } [10] => class SimpleXMLElement#12 (3) { public $matauang => string(3) "CHF" public $beli => string(9) "14,149.00" public $jual => string(9) "14,709.00" } [11] => class SimpleXMLElement#13 (3) { public $matauang => string(3) "USD" public $beli => string(9) "13,982.00" public $jual => string(9) "14,474.00" } } }
untuk mengambil kurs beli, bisa langsung di looping dengan
<?php $xmlurl = file_get_contents("http://localhost:9900/kurs.xml"); $xml = simplexml_load_string($xmlurl, "SimpleXMLElement", LIBXML_NOCDATA); $kursBeli = []; $kursJual = []; foreach ($xml->rate as $data) { $kursBeli[] = $data->beli; $kursJual[] = $data->jual; } echo $kursBeli[0]; // 9,276.00
Cara lain, gunakan bantuan json_encode
dan json_decode
untuk ‘membersihkan’ data yang tidak perlu dari XML diatas.
<?php // parsing-xml.php $xmlurl = file_get_contents("http://localhost:9900/kurs.xml"); $xml = simplexml_load_string($xmlurl, "SimpleXMLElement", LIBXML_NOCDATA); $json = json_encode($xml); $array = json_decode($json, TRUE); var_dump($array);
jalankan lagi script diatas
array(1) { 'rate' => array(12) { [0] => array(3) { 'matauang' => string(3) "AUD" 'beli' => string(9) " 9,276.00" 'jual' => string(9) " 9,757.00" } [1] => array(3) { 'matauang' => string(3) "GBP" 'beli' => string(9) "17,033.00" 'jual' => string(9) "17,581.00" } [2] => array(3) { 'matauang' => string(3) "CAD" 'beli' => string(9) "10,343.00" 'jual' => string(9) "10,879.00" } [3] => array(3) { 'matauang' => string(3) "CNY" 'beli' => string(8) "1,790.43" 'jual' => string(8) "2,148.52" } [4] => array(3) { 'matauang' => string(3) "EUR" 'beli' => string(9) "15.420.00" 'jual' => string(9) "16,068.00" } [5] => array(3) { 'matauang' => string(3) "HKD" 'beli' => string(8) "1,663.62" 'jual' => string(8) "1,839.98" } [6] => array(3) { 'matauang' => string(3) "JPY" 'beli' => string(6) "130.76" 'jual' => string(6) "137.89" } [7] => array(3) { 'matauang' => string(3) "NZD" 'beli' => string(8) "8,699.00" 'jual' => string(8) "9,183.00" } [8] => array(3) { 'matauang' => string(3) "SGD" 'beli' => string(9) " 9,966.00" 'jual' => string(9) "10,429.00" } [9] => array(3) { 'matauang' => string(3) "SEK" 'beli' => string(8) "1,439.00" 'jual' => string(8) "1,497.00" } [10] => array(3) { 'matauang' => string(3) "CHF" 'beli' => string(9) "14,149.00" 'jual' => string(9) "14,709.00" } [11] => array(3) { 'matauang' => string(3) "USD" 'beli' => string(9) "13,982.00" 'jual' => string(9) "14,474.00" } } }
sudah jauh lebih mudah. Karena datanya sudah bersih dari tag ataupun sesuatu yang tidak dibutuhkan.