Cara menggunakan can php read xml?

Untuk mengolah data berbentuk XML menjadi informasi yang mudah dibaca, anda harus melakukan parsing terhadap file XML tersebut. Anda dapat menggunakan bahasa pemrograman apapun. Di sini saya memberi contoh melakukan parsing XML secara sederhana dengan menggunakan PHP. XML ini di dapat dari RSS feed Yahoo yang berisi tentang berita dunia. Untuk melihat XML nya anda dapat membuka link berikut.

There is a common "trick" often proposed to convert a SimpleXML object to an array, by running it through json_encode() and then json_decode(). I'd like to explain why this is a bad idea.

Most simply, because the whole point of SimpleXML is to be easier to use and more powerful than a plain array. For instance, you can write bar->baz['bing'] ?> and it means the same thing as bar[0]->baz[0]['bing'] ?>, regardless of how many bar or baz elements there are in the XML; and if you write bar[0]->baz[0] ?> you get all the string content of that node - including CDATA sections - regardless of whether it also has child elements or attributes. You also have access to namespace information, the ability to make simple edits to the XML, and even the ability to "import" into a DOM object, for much more powerful manipulation. All of this is lost by turning the object into an array rather than reading understanding the examples on this page.

Additionally, because it is not designed for this purpose, the conversion to JSON and back will actually lose information in some situations. For instance, any elements or attributes in a namespace will simply be discarded, and any text content will be discarded if an element also has children or attributes. Sometimes, this won't matter, but if you get in the habit of converting everything to arrays, it's going to sting you eventually.

Of course, you could write a smarter conversion, which didn't have these limitations, but at that point, you are getting no value out of SimpleXML at all, and should just use the lower level XML Parser functions, or the XMLReader class, to create your structure. You still won't have the extra convenience functionality of SimpleXML, but that's your loss.

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

Tulisan menarik lainnya

  • Parsing File CSV dan Import ke MySQL dengan PHP

    File CSV yang kita miliki adalah data asal, karena hanya dibuat sebagai contoh pengolahan data…

  • Script untuk Login ke Webiste dengan PHP CURL

    Sebagai contoh kita akan menggunakan script di tutorial Membuat Script Login Sederhana dengan PHP dan…

  • Mengimport File Excel Ke MySQL dengan Script PHP

    Kita akan menggunakan library PHP Excel Reader, untuk keperluan percobaan ini saya menyertakan script dan…

Postingan terbaru

LIHAT SEMUA