溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

simplexml_load_file函數(shù)怎么在php中使用

發(fā)布時間:2020-12-30 15:24:43 來源:億速云 閱讀:148 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)simplexml_load_file函數(shù)怎么在php中使用,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

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

例子,XML文件代碼如下:

復(fù)制代碼 代碼如下:

<?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 代碼如下:

復(fù)制代碼 代碼如下:

<?php 
if (file_exists('test.xml')) 

  $xml = simplexml_load_file('test.xml'); 
  var_dump($xml); 

else 

  exit('Error.'); 

?>


 
運行輸出結(jié)果如下: 

復(fù)制代碼 代碼如下:


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”文件,其內(nèi)容如下:

復(fù)制代碼 代碼如下:

<?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>日落西山時,天空出現(xiàn)了一個移動的發(fā)亮物體。</trans> 
 </sent> 
 <sent> 
  <orig>A bright rainbow arched above.</orig> 
  <trans>一彎明亮的彩虹懸掛在天空。</trans> 
 </sent> 
</dict>


在PHP語言中我們可以用以下方法取得我們想要的值: 

復(fù)制代碼 代碼如下:

<?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; 

?>

“第一部分”將輸出: 

復(fù)制代碼 代碼如下:


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] => 日落西山時,天空出現(xiàn)了一個移動的發(fā)亮物體。
                )

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

        )

)

“第二部分”將輸出: 

復(fù)制代碼 代碼如下:


例句: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.
翻譯:日落西山時,天空出現(xiàn)了一個移動的發(fā)亮物體。
例句:A bright rainbow arched above.
翻譯:一彎明亮的彩虹懸掛在天空。

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

復(fù)制代碼 代碼如下:

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

以上就是simplexml_load_file函數(shù)怎么在php中使用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI