当前位置:首页 > 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. ["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”文件,其内容如下:

  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. 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] => 日落西山时,天空出现了一个移动的发亮物体。 
  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. 翻译:日落西山时,天空出现了一个移动的发亮物体。 
  100. 例句:A bright rainbow arched above. 
  101. 翻译:一弯明亮的彩虹悬挂在天空。 

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

  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函数使用

分享到: