Tuesday, May 22nd

Last update:09:01:51 PM GMT

Headlines:

You are here: Home Programming PHP PHP วิธีโชว์ค่าจาก XML ง่ายๆด้วย SimpleXML

PHP วิธีโชว์ค่าจาก XML ง่ายๆด้วย SimpleXML

PHP Codingหากใครที่ใช้ PHP 5.0 จะทราบว่ามี คำสั่งง่ายๆตัวหนึ่งใน Library ของมันคือ SimpleXML
นี่เป็นตัวอย่างที่ จะอธิบายได้เห็นภาพมากที่สุด จากเว็บไซต์ w3schools

ก่อนอื่น เตรียมไฟล์ XML ซึ่งถ้ามาชำแหละ จะพบว่า

 

<?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. แสดงผลออกมาเลย

<?php 
$xml = simplexml_load_file("test.xml"); 
 
echo $xml->getName() . "<br />"
 
foreach($xml->children() as $child
  { 
  echo $child->getName() . ": " . $child . "<br />"
  } 
?>

หน้าจอการแสดงผลก็จะออกมาอย่างที่เห็นครับ

note 
to: Tove 
from: Jani 
heading: Reminder 
body: Don't forget me this weekend!

ง่ายๆ สบายๆ ไปหามาให้ดู กันตาม ประสา nomad?
ไป post กันได้ที่ Community นะครับ

 

http://www.daydev.com/index.php/community.html?func=view&id=12&catid=6

Powered by Web Agency
comments

Comments and Reviews