当前位置:首页 > PHP教程 > php函数 > 列表

php中simplexml_load_file函数使用方法

发布:smiling 来源: PHP粉丝网  添加日期:2014-09-12 15:24:33 浏览: 评论:0 

在php中simplexml_load_file() 函数把 XML 文档载入对象中之后我们就可以利用由此函数返回回的对象进行相关的操作了,下面我们看几个测试实例.

例子,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. { 
  18. ["to"]=> string(4) "George" 
  19. ["from"]=> string(4) "John" 
  20. ["heading"]=> string(8) "Reminder" 
  21. ["body"]=> string(29) "Don't forget the meeting!" 
  22. } 

假如有一个“iciba.xml”文件,其内容如下:

  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>日落西山时,天空出现了一个移动的发亮物体。</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.  
  43. SimpleXMLElement Object 
  44. ( 
  45.     [@attributes] => Array 
  46.         ( 
  47.             [num] => 219 
  48.             [id] => 219 
  49.             [name] => 219 
  50.         ) 
  51.  
  52.     [key] => 天空 
  53.     [pos] => SimpleXMLElement Object 
  54.         ( 
  55.         ) 
  56.  
  57.     [acceptation] => Array;Array; 
  58.     [sent] => Array 
  59.         ( 
  60.             [0] => SimpleXMLElement Object 
  61.                 ( 
  62.                     [orig] => The church tower stood against the sky like a finger pointing towards heaven. 
  63.                     [trans] => 教堂的尖塔在天空的映衬下宛如指向天空的手指。 
  64.                 ) 
  65.  
  66.             [1] => SimpleXMLElement Object 
  67.                 ( 
  68.                     [orig] => A balloon floated across the sky. 
  69.                     [trans] => 气球飘过天空。 
  70.                 ) 
  71.  
  72.             [2] => SimpleXMLElement Object 
  73.                 ( 
  74.                     [orig] => A bolt of lightning lit up the sky. 
  75.                     [trans] => (一道)闪电照亮了天空。 
  76.                 ) 
  77.  
  78.             [3] => SimpleXMLElement Object 
  79.                 ( 
  80.                     [orig] => A bright moving object appeared in the sky at sunset. 
  81.                     [trans] => 日落西山时,天空出现了一个移动的发亮物体。 
  82.                 ) 
  83.  
  84.             [4] => SimpleXMLElement Object 
  85.                 ( 
  86.                     [orig] => A bright rainbow arched above. 
  87.                     [trans] => 一弯明亮的彩虹悬挂在天空。 
  88.                 ) 
  89.  
  90.         ) 
  91.  
  92. )“第二部分”将输出: 
  93.  
  94. 例句:The church tower stood against the sky like a finger pointing towards heaven. 
  95. 翻译:教堂的尖塔在天空的映衬下宛如指向天空的手指。 
  96. 例句:A balloon floated across the sky. 
  97. 翻译:气球飘过天空。 
  98. 例句:A bolt of lightning lit up the sky. 
  99. 翻译:(一道)闪电照亮了天空。 
  100. 例句:A bright moving object appeared in the sky at sunset. 
  101. 翻译:日落西山时,天空出现了一个移动的发亮物体。 
  102. 例句:A bright rainbow arched above. 
  103. 翻译:一弯明亮的彩虹悬挂在天空。 

例子,更深入的一个遍历输出生成表格,代码如下:

  1. eader("content-type:text/html; charset=utf-8"); //设置编码 
  2. $xml = simplexml_load_file('a.xml'); //载入xml文件 $lists和xml文件的根节点是一样的 
  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>循环读取:<br>"; 
  9. foreach($xml->user as $users){ //有多个user,取得的是数组,循环输出 
  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. }//开源代码phpfensi.com 
  16.  
  17. echo "<br>循环读取:<br>"; 
  18. foreach($xml->town as $towns){ //有多个user,取得的是数组,循环输出 
  19.     echo "-------------------<br>"; 
  20.     echo "id:".$towns['id']."<br>"; 
  21.     echo "归属:".$towns['parent']."<br>"; 
  22.     echo "地区:".$towns."<br>"; 
  23. }

Tags: simplexml_load_file php函数使用

分享到: