simplexml_load_file
將XML 文檔轉換為對象。
simplexml_load_file()
函數將XML 文檔轉換為對象。
將XML 文件轉換為對象,然後輸出對象的鍵和元素:
<?php $xml = simplexml_load_file ( "note.xml" ) ; print_r ( $xml ) ; ?>
運行實例
假設我們有以下XML 文件" note.xml ":
<?xml version="1.0" encoding="UTF-8"?> < note > < to > George </ to > < from > John </ from > < heading > Reminder </ heading > < body > Don't forget me this weekend! </ body > </ note >
輸出XML 文件中每個元素的數據:
<?php $xml = simplexml_load_file ( "note.xml" ) ; echo $xml -> to . "<br>" ; echo $xml -> from . "<br>" ; echo $xml -> heading . "<br>" ; echo $xml -> body ; ?>
運行實例
輸出XML 文件中每個子節點的元素名稱和數據:
<?php $xml = simplexml_load_file ( "note.xml" ) ; echo $xml -> getName ( ) . "<br>" ; foreach ( $xml -> children ( ) as $child ) { echo $child -> getName ( ) . ": " . $child . "<br>" ; } ?>
運行實例
simplexml_load_file ( file , class , options , ns , is_prefix )
參數 | 描述 |
---|---|
file | 必需。指定XML 文件的路徑。 |
class | 可選。指定新對象的類名。 |
options |
可選。指定額外的Libxml 參數。通過指定選項和1 或0(TRUE 或FALSE,例如LIBXML_NOBLANKS(1))來設置。 可能的值包括:
|
ns | 可選。指定命名空間前綴或URI。 |
is_prefix |
可選。指定布爾值。如果ns是前綴,則為TRUE;如果ns是URI,則為FALSE。 默認為FALSE。 |