在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.  
  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!" 

假如有一個(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語言中我們可以用以下方法取得我們想要的值: 
  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.phpfensi.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] => 日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(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. 翻譯:日落西山時(shí),天空出現(xiàn)了一個(gè)移動(dòng)的發(fā)亮物體。 
  100. 例句:A bright rainbow arched above. 
  101. 翻譯:一彎明亮的彩虹懸掛在天空。 

例子,更深入的一個(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. }//開源代碼phpfensi.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>"
  23. }
轉(zhuǎn)載請(qǐng)注明來源:php中simplexml_load_file函數(shù)使用方法

  哈爾濱品用軟件有限公司致力于為哈爾濱的中小企業(yè)制作大氣、美觀的優(yōu)秀網(wǎng)站,并且能夠搭建符合百度排名規(guī)范的網(wǎng)站基底,使您的網(wǎng)站無需額外費(fèi)用,即可穩(wěn)步提升排名至首頁。歡迎體驗(yàn)最佳的哈爾濱網(wǎng)站建設(shè)。