PHP simplexml_load_file() 函數
定義和用法
simplexml_load_file() 函數把 XML 文件載入對象中。
如果失败,則返回 false。
语法
simplexml_load_file(file,class,options,ns,is_prefix)
参数 | 描述 |
---|---|
file | จำเป็น กำหนดเอกสาร XML ที่ใช้ |
class | ใช้ได้เลือก กำหนดคลาสของออปเจ็กท์ใหม่ |
options | ใช้ได้เลือก กำหนดค่าพิเศษของ Libxml |
ns | ใช้ได้เลือก |
is_prefix | ใช้ได้เลือก |
ค่าที่กลับมา
กลับมาเป็นออปเจ็กท์ของ SimpleXMLElement ซึ่งมีคุณสมบัติที่มีในเอกสาร XML ถ้าล้มเหลวก็กลับมาเป็น false。
ตัวอย่าง
ไฟล์ XML:
<?xml version="1.0" encoding="ISO-8859-1"?> <note> <to>George</to> <from>John</from> <heading>Reminder</heading> <body>Don't forget the meeting!</body> </note>
PHP โค้ด:
<?php if (file_exists('test.xml')) { $xml = simplexml_load_file('test.xml'); var_dump($xml); } else { exit('Error.'); } ?>
การแสดงผล:
object(SimpleXMLElement)#1 (4) { ["to"]=> string(4) "George" ["from"]=> string(4) "John" ["heading"]=> string(8) "Reminder" ["body"]=> string(29) "Don't forget the meeting!" }