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

首頁 > 語言 > PHP > 正文

php中simplexml_load_file函數(shù)用法實(shí)例

2024-09-04 11:43:10
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php中simplexml_load_file函數(shù)用法,以實(shí)例形式詳細(xì)的講述了simplexml_load_file函數(shù)讀取XML文件的具體方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。

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

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

例子,XML文件代碼如下:

  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <note> 
  3. <to>George</to> 
  4. <from>John</from> 
  5. <heading>Reminder</heading> 
  6. <body>Don't forget the meeting!</body> 
  7. </note> 

PHP 代碼如下:

  1. <?php  
  2. if (file_exists('test.xml'))  
  3. {  
  4.   $xml = simplexml_load_file('test.xml');  
  5.   var_dump($xml);  
  6. }  
  7. else  
  8. {  
  9.   exit('Error.');  
  10. }  
  11. ?> 

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

  1. object(SimpleXMLElement)#1 (4) { 
  2.   ["to"]=> 
  3.   string(6) "George" 
  4.   ["from"]=> 
  5.   string(4) "John" 
  6.   ["heading"]=> 
  7.   string(8) "Reminder" 
  8.   ["body"]=> 
  9.   string(25) "Don't forget the meeting!" 

假如有一個(gè)“iciba.xml”文件,其內(nèi)容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <dict num="219" id="219" name="219">  
  3.  <key>天空</key>  
  4.  <pos></pos>  
  5.  <acceptation>Array;Array;</acceptation>  
  6.  <sent>  
  7.   <orig>The church tower stood against the sky like a finger pointing towards heaven.</orig>  
  8.   <trans>教堂的尖塔在天空的映襯下宛如指向天空的手指。</trans>  
  9.  </sent>  
  10.  <sent>  
  11.   <orig>A balloon floated across the sky.</orig>  
  12.   <trans>氣球飄過天空。</trans>  
  13.  </sent>  
  14.  <sent>  
  15.   <orig>A bolt of lightning lit up the sky.</orig>  
  16.   <trans>(一道)閃電照亮了天空。</trans>  
  17.  </sent>  
  18.  <sent>  
  19.   <orig>A bright moving object appeared in the sky at sunset.</orig>  
  20.   <trans>日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。</trans>  
  21.  </sent>  
  22.  <sent>  
  23.   <orig>A bright rainbow arched above.</orig>  
  24.   <trans>一彎明亮的彩虹懸掛在天空。</trans>  
  25.  </sent>  
  26. </dict> 

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

  1. <?php  
  2. $xmldata = simplexml_load_file("iciba.xml");  
  3.  
  4. header("Content-Type: text/html; charset=UTF-8");  
  5. print_r($xmldata); //第一部分  
  6.  
  7. $listcount = count($xmldata->sent);  
  8.  
  9. for($i=0;$i<$listcount;$i++){ //第二部分  
  10.  $dictlist = $xmldata->sent[$i];  
  11.  echo "<br />例句:".$dictlist->orig;  
  12.  echo "<br />翻譯:".$dictlist->trans;  
  13. }  
  14. ?> 

“第一部分”將輸出: 

  1. SimpleXMLElement Object 
  2.     [@attributes] => Array 
  3.         ( 
  4.             [num] => 219 
  5.             [id] => 219 
  6.             [name] => 219 
  7.         ) 
  8.     [key] => 天空 
  9.     [pos] => SimpleXMLElement Object 
  10.         ( 
  11.         ) 
  12.  
  13.     [acceptation] => Array;Array; 
  14.     [sent] => Array 
  15.         ( 
  16.             [0] => SimpleXMLElement Object 
  17.                 ( 
  18.                     [orig] => The church tower stood against the sky like a finger pointing towards heaven. 
  19.                     [trans] => 教堂的尖塔在天空的映襯下宛如指向天空的手指。 
  20.                 ) 
  21.  
  22.             [1] => SimpleXMLElement Object 
  23.                 ( 
  24.                     [orig] => A balloon floated across the sky. 
  25.                     [trans] => 氣球飄過天空。 
  26.                 ) 
  27.  
  28.             [2] => SimpleXMLElement Object 
  29.                 ( 
  30.                     [orig] => A bolt of lightning lit up the sky. 
  31.                     [trans] => (一道)閃電照亮了天空。 
  32.                 ) 
  33.  
  34.             [3] => SimpleXMLElement Object 
  35.                 ( 
  36.                     [orig] => A bright moving object appeared in the sky at sunset. 
  37.                     [trans] => 日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。 
  38.                 ) 
  39.  
  40.             [4] => SimpleXMLElement Object 
  41.                 ( 
  42.                     [orig] => A bright rainbow arched above. 
  43.                     [trans] => 一彎明亮的彩虹懸掛在天空。 
  44.                 ) 
  45.  
  46.         ) 
  47.  

“第二部分”將輸出:

例句: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.

翻譯:日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。

例句:A bright rainbow arched above.

翻譯:一彎明亮的彩虹懸掛在天空。

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

  1. eader("content-type:text/html; charset=utf-8"); //設(shè)置編碼  
  2. $xml = simplexml_load_file('a.xml'); //載入xml文件 $lists和xml文件的根節(jié)點(diǎn)是一樣的  
  3. echo $xml->company."<br>";  
  4. echo $xml->town."<br>id:";  
  5. echo $xml->town['id']."<br>parent:";  
  6. echo $xml->town['parent']."<br>";  
  7.  
  8. echo "<br>循環(huán)讀取:<br>";  
  9. foreach($xml->user as $users){ //有多個(gè)user,取得的是數(shù)組,循環(huán)輸出  
  10.     echo "-------------------<br>";  
  11.     echo "姓名:".$users->name."<br>";  
  12.     echo "編號(hào):".$users->age."<br>";  
  13.     echo "性別:".$users->age['sex']."<br>";  
  14.     echo "序號(hào):".$users->height."<br>";  
  15. //www.survivalescaperooms.com 
  16.  
  17. echo "<br>循環(huán)讀取:<br>";  
  18. foreach($xml->town as $towns){ //有多個(gè)user,取得的是數(shù)組,循環(huán)輸出  
  19.     echo "-------------------<br>";  
  20.     echo "id:".$towns['id']."<br>";  
  21.     echo "歸屬:".$towns['parent']."<br>";  
  22.     echo "地區(qū):".$towns."<br>";  

希望本文所述對(duì)大家的PHP程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大丰市| 鄂温| 易门县| 澎湖县| 酒泉市| 大安市| 黑龙江省| 伊宁县| 泗水县| 安福县| 九寨沟县| 朔州市| 凉城县| 莱州市| 台南县| 横山县| 大新县| 南岸区| 黄大仙区| 调兵山市| 青浦区| 都匀市| 灌阳县| 苍溪县| 黎川县| 平昌县| 井冈山市| 合川市| 河南省| 阳信县| 丽水市| 偃师市| 武义县| 萍乡市| 衡山县| 山东省| 内丘县| 东乌珠穆沁旗| 江陵县| 盘锦市| 许昌市|