国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

php中simplexml_load_file函數用法實例

2024-05-04 23:27:13
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了php中simplexml_load_file函數用法,以實例形式詳細的講述了simplexml_load_file函數讀取XML文件的具體方法,非常具有實用價值,需要的朋友可以參考下
 
 

本文實例講述了php中simplexml_load_file函數用法。分享給大家供大家參考。具體用法分析如下:

在php中simplexml_load_file() 函數把 XML 文檔載入對象中之后我們就可以利用由此函數返回的對象進行相關的操作了,下面我們看幾個測試實例.

例子,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(6) "George"
  ["from"]=>
  string(4) "John"
  ["heading"]=>
  string(8) "Reminder"
  ["body"]=>
  string(25) "Don't forget the meeting!"
}

假如有一個“iciba.xml”文件,其內容如下:
復制代碼代碼如下:
<?xml version="1.0" encoding="UTF-8"?>  
<dict num="219" id="219" name="219">  
 <key>天空</key>  
 <pos></pos>  
 <acceptation>Array;Array;</acceptation>  
 <sent>  
  <orig>The church tower stood against the sky like a finger pointing towards heaven.</orig>  
  <trans>教堂的尖塔在天空的映襯下宛如指向天空的手指。</trans>  
 </sent>  
 <sent>  
  <orig>A balloon floated across the sky.</orig>  
  <trans>氣球飄過天空。</trans>  
 </sent>  
 <sent>  
  <orig>A bolt of lightning lit up the sky.</orig>  
  <trans>(一道)閃電照亮了天空。</trans>  
 </sent>  
 <sent>  
  <orig>A bright moving object appeared in the sky at sunset.</orig>  
  <trans>日落西山時,天空出現了一個移動的發亮物體。</trans>  
 </sent>  
 <sent>  
  <orig>A bright rainbow arched above.</orig>  
  <trans>一彎明亮的彩虹懸掛在天空。</trans>  
 </sent>  
</dict>

在PHP語言中我們可以用以下方法取得我們想要的值:  
復制代碼代碼如下:
<?php  
$xmldata = simplexml_load_file("iciba.xml");  
  
header("Content-Type: text/html; charset=UTF-8");  
print_r($xmldata); //第一部分  
  
$listcount = count($xmldata->sent);  
  
for($i=0;$i<$listcount;$i++){ //第二部分  
 $dictlist = $xmldata->sent[$i];  
 echo "<br />例句:".$dictlist->orig;  
 echo "<br />翻譯:".$dictlist->trans;  
}  
?>

 

“第一部分”將輸出:  

復制代碼代碼如下:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [num] => 219
            [id] => 219
            [name] => 219
        )

 

    [key] => 天空
    [pos] => SimpleXMLElement Object
        (
        )

    [acceptation] => Array;Array;
    [sent] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [orig] => The church tower stood against the sky like a finger pointing towards heaven.
                    [trans] => 教堂的尖塔在天空的映襯下宛如指向天空的手指。
                )

            [1] => SimpleXMLElement Object
                (
                    [orig] => A balloon floated across the sky.
                    [trans] => 氣球飄過天空。
                )

            [2] => SimpleXMLElement Object
                (
                    [orig] => A bolt of lightning lit up the sky.
                    [trans] => (一道)閃電照亮了天空。
                )

            [3] => SimpleXMLElement Object
                (
                    [orig] => A bright moving object appeared in the sky at sunset.
                    [trans] => 日落西山時,天空出現了一個移動的發亮物體。
                )

            [4] => SimpleXMLElement Object
                (
                    [orig] => A bright rainbow arched above.
                    [trans] => 一彎明亮的彩虹懸掛在天空。
                )

        )

)

 

“第二部分”將輸出:  

復制代碼代碼如下:

例句:The church tower stood against the sky like a finger pointing towards heaven.
翻譯:教堂的尖塔在天空的映襯下宛如指向天空的手指。
例句:A balloon floated across the sky.
翻譯:氣球飄過天空。
例句:A bolt of lightning lit up the sky.
翻譯:(一道)閃電照亮了天空。
例句:A bright moving object appeared in the sky at sunset.
翻譯:日落西山時,天空出現了一個移動的發亮物體。
例句:A bright rainbow arched above.
翻譯:一彎明亮的彩虹懸掛在天空。

 

例子,更深入的一個遍歷輸出生成表格,代碼如下:

復制代碼代碼如下:
eader("content-type:text/html; charset=utf-8"); //設置編碼  
$xml = simplexml_load_file('a.xml'); //載入xml文件 $lists和xml文件的根節點是一樣的  
echo $xml->company."<br>";  
echo $xml->town."<br>id:";  
echo $xml->town['id']."<br>parent:";  
echo $xml->town['parent']."<br>";  
  
echo "<br>循環讀取:<br>";  
foreach($xml->user as $users){ //有多個user,取得的是數組,循環輸出  
    echo "-------------------<br>";  
    echo "姓名:".$users->name."<br>";  
    echo "編號:".$users->age."<br>";  
    echo "性別:".$users->age['sex']."<br>";  
    echo "序號:".$users->height."<br>";  
}
  
echo "<br>循環讀取:<br>";  
foreach($xml->town as $towns){ //有多個user,取得的是數組,循環輸出  
    echo "-------------------<br>";  
    echo "id:".$towns['id']."<br>";  
    echo "歸屬:".$towns['parent']."<br>";  
    echo "地區:".$towns."<br>";  
}

 

希望本文所述對大家的PHP程序設計有所幫助。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 常州市| 边坝县| 台北市| 太仆寺旗| 霸州市| 郎溪县| 荔波县| 晋宁县| 曲麻莱县| 安宁市| 泾阳县| 夏津县| 兖州市| 砀山县| 开平市| 河南省| 苏尼特左旗| 芜湖县| 当雄县| 黄梅县| 湘阴县| 嘉鱼县| 海淀区| 民权县| 高碑店市| 龙川县| 佛冈县| 丁青县| 社会| 松江区| 上蔡县| 拜泉县| 淮滨县| 望江县| 河北区| 铜陵市| 磐安县| 濮阳县| 班戈县| 搜索| 石河子市|