simplexml_load_file()函數是PHP中的內置函數,用於將格式良好的XML文檔轉換為給定文件的對象。
用法:
SimpleXMLElement simplexml_load_file( string $filename, string $class_name = "SimpleXMLElement",
                                    int $options = 0, string $ns = "", bool $is_prefix = FALSE )
參數:該函數接受上述和以下所述的五個參數:
- $filename:此參數保存文件名的路徑。
- $class_name:它是可選參數。使用simplexml_load_file()函數返回指定類的對象。該類擴展了SimpleXMLElement類。
- $options:它是可選參數,用於其他Libxml參數。
- $ns:此參數保存名稱空間前綴或URI。
- $is_prefix:如果ns為前綴,則此參數設置為TRUE,如果為URI,則此參數設置為FALSE。其默認值為FALSE。
返回值:該函數返回SimpleXMLElement類的對象,該對象的屬性包含XML文檔中保存的數據,如果失敗,則返回FALSE。
以下示例程序旨在說明PHP中的simplexml_load_file()函數:
gfg.xml文件:
<?xml version="1.0"?> 
<organization> 
    <name>GeeksforGeeks</name> 
    <address>Noida India</address> 
    <contact> 
        <email>abc@geeksforgeeks.org</email> 
        <mobile>+91-987654321</mobile> 
    </contact> 
</organization>程序:
<?php 
  
// Check file exist or not 
if (file_exists('gfg.xml')) { 
      
    // If XML file exists then 
    // load the XML file 
    $xml_file = simplexml_load_file('gfg.xml'); 
   
    // Display the content of XML file 
    var_dump($xml_file); 
      
} else { 
      
    exit('Fail to open the file'); 
} 
?>輸出:
object(SimpleXMLElement)#1 (3) { 
    ["name"]=> string(13) "GeeksforGeeks" 
    ["address"]=> string(11) "Noida India" 
    ["contact"]=> object(SimpleXMLElement)#2 (2) {
        ["email"]=> string(21) "abc@geeksforgeeks.org" 
        ["mobile"]=> string(13) "+91-987654321" 
    }
}
參考: https://www.php.net/manual/en/function.simplexml-load-file.php
相關用法
- PHP cos( )用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- d3.js d3.map.set()用法及代碼示例
- PHP each()用法及代碼示例
- PHP end()用法及代碼示例
- PHP pos()用法及代碼示例
- PHP key()用法及代碼示例
- PHP sin( )用法及代碼示例
- CSS var()用法及代碼示例
- PHP abs()用法及代碼示例
- p5.js min()用法及代碼示例
- PHP each()用法及代碼示例
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | simplexml_load_file() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
