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

首頁 > 語言 > PHP > 正文

php中simplexml_load_file函數(shù)使用方法

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

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

例子,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.  
  8. else 
  9.   { 
  10.   exit('Error.'); 
  11.   } 
  12. ?> 
  13.  
  14. //輸出: 
  15.  
  16. object(SimpleXMLElement)#1 (4) 
  17. ["to"]=> string(4) "George" 
  18. ["from"]=> string(4) "John" 
  19. ["heading"]=> string(8) "Reminder" 
  20. ["body"]=> string(29) "Don't forget the meeting!" 

假如有一個“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>日落西山時,天空出現(xiàn)了一個移動的發(fā)亮物體。</trans> 
  21.  </sent> 
  22.  <sent> 
  23.   <orig>A bright rainbow arched above.</orig> 
  24.   <trans>一彎明亮的彩虹懸掛在天空。</trans> 
  25.  </sent> 
  26. </dict>在PHP語言中我們可以用以下方法取得我們想要的值: 
  27.  
  28. <?php 
  29. $xmldata = simplexml_load_file("iciba.xml"); 
  30.  
  31. header("Content-Type: text/html; charset=UTF-8"); 
  32. print_r($xmldata); //第一部分www.survivalescaperooms.com 
  33.  
  34. $listcount = count($xmldata->sent); 
  35.  
  36. for($i=0;$i<$listcount;$i++){ //第二部分 
  37.  $dictlist = $xmldata->sent[$i]; 
  38.  echo "<br />例句:".$dictlist->orig; 
  39.  echo "<br />翻譯:".$dictlist->trans; 
  40. ?>“第一部分”將輸出: 
  41.  
  42. SimpleXMLElement Object 
  43.     [@attributes] => Array 
  44.         ( 
  45.             [num] => 219 
  46.             [id] => 219 
  47.             [name] => 219 
  48.         ) 
  49.  
  50.     [key] => 天空 
  51.     [pos] => SimpleXMLElement Object 
  52.         ( 
  53.         ) 
  54.  
  55.     [acceptation] => Array;Array; 
  56.     [sent] => Array 
  57.         ( 
  58.             [0] => SimpleXMLElement Object 
  59.                 ( 
  60.                     [orig] => The church tower stood against the sky like a finger pointing towards heaven. 
  61.                     [trans] => 教堂的尖塔在天空的映襯下宛如指向天空的手指。 
  62.                 ) 
  63.  
  64.             [1] => SimpleXMLElement Object 
  65.                 ( 
  66.                     [orig] => A balloon floated across the sky. 
  67.                     [trans] => 氣球飄過天空。 
  68.                 ) 
  69.  
  70.             [2] => SimpleXMLElement Object 
  71.                 ( 
  72.                     [orig] => A bolt of lightning lit up the sky. 
  73.                     [trans] => (一道)閃電照亮了天空。 
  74.                 ) 
  75.  
  76.             [3] => SimpleXMLElement Object 
  77.                 ( 
  78.                     [orig] => A bright moving object appeared in the sky at sunset. 
  79.                     [trans] => 日落西山時,天空出現(xiàn)了一個移動的發(fā)亮物體。 
  80.                 ) 
  81.  
  82.             [4] => SimpleXMLElement Object 
  83.                 ( 
  84.                     [orig] => A bright rainbow arched above. 
  85.                     [trans] => 一彎明亮的彩虹懸掛在天空。 
  86.                 ) 
  87.  
  88.         ) 
  89.  
  90. )“第二部分”將輸出: 
  91.  
  92. 例句:The church tower stood against the sky like a finger pointing towards heaven. 
  93. 翻譯:教堂的尖塔在天空的映襯下宛如指向天空的手指。 
  94. 例句:A balloon floated across the sky. 
  95. 翻譯:氣球飄過天空。 
  96. 例句:A bolt of lightning lit up the sky. 
  97. 翻譯:(一道)閃電照亮了天空。 
  98. 例句:A bright moving object appeared in the sky at sunset. 
  99. 翻譯:日落西山時,天空出現(xiàn)了一個移動的發(fā)亮物體。 
  100. 例句:A bright rainbow arched above. 
  101. 翻譯:一彎明亮的彩虹懸掛在天空。 

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 广宗县| 玛多县| 屏山县| 西乡县| 海南省| 牟定县| 汝州市| 张北县| 嘉定区| 内乡县| 永清县| 马关县| 平顶山市| 新田县| 玉林市| 阳江市| 顺义区| 彩票| 永泰县| 阿拉善右旗| 陵川县| 周宁县| 靖远县| 新安县| 灵山县| 汨罗市| 株洲市| 丰镇市| 灌南县| 福安市| 周口市| 新兴县| 吉林省| 昔阳县| 咸宁市| 开原市| 漠河县| 海口市| 汝阳县| 获嘉县| 全州县|