หากใครที่ใช้ PHP 5.0 จะทราบว่ามี คำสั่งง่ายๆตัวหนึ่งใน Library ของมันคือ SimpleXML
นี่เป็นตัวอย่างที่ จะอธิบายได้เห็นภาพมากที่สุด จากเว็บไซต์ w3schools
ก่อนอื่น เตรียมไฟล์ XML ซึ่งถ้ามาชำแหละ จะพบว่า
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> |
และก็กระทำการต่อไปนี้:
1. โหลดไฟล์ XML
2. เก็บชื่อของตัวแปร element
3. เก็บ loop และ ใช้ฟังก์ชั่น children()
4. แสดงผลออกมาเลย
1 2 3 4 5 6 7 8 |
<?php $xml = simplexml_load_file("test.xml"); echo $xml->getName() . "<br />"; foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; } ?> |
หน้าจอการแสดงผลก็จะออกมาอย่างที่เห็นครับ
1 2 3 4 5 |
note to: Tove from: Jani heading: Reminder body: Don't forget me <span class="keyword">this</span> weekend! |
ง่ายๆ สบายๆ ไปหามาให้ดู กันตาม ประสา nomad?
ไป post กันได้ที่ Community นะครับ
http://www.daydev.com/index.php/community.html?func=view&id=12&catid=6