当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP simplexml使用方法详解

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-20 13:48:29 浏览: 评论:0 

本文章给来大家讲一下关于PHP下simplexml使用方法,希望通过此文章能提升大家对simplexml操作xml文档的理解,有需要了解的朋友可参考.

PHP XML处理XML代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?> 
  2. <!-- Edited with XML Spy v2007 (http://www.phpfensi.com) --> 
  3. <breakfast_menu> 
  4.  <food id="1"> 
  5.   <name>Belgian Waffles</name> 
  6.   <price>$5.95</price> 
  7.   <description>two of our famous Belgian Waffles with plenty of real maple syrup</description> 
  8.   <calories>650</calories> 
  9.  </food> 
  10.  <food id="2"> 
  11.   <name>Strawberry Belgian Waffles</name> 
  12.   <price>$7.95</price> 
  13.   <description>light Belgian waffles covered with strawberries and whipped cream</description> 
  14.   <calories>900</calories> 
  15.  </food> 
  16.  <food id="3"> 
  17.   <name>Berry-Berry Belgian Waffles</name> 
  18.   <price>$8.95</price> 
  19.   <description>light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> 
  20.   <calories>900</calories> 
  21.  </food> 
  22. </breakfast_menu> 

php解决xml代码,代码如下:

  1. <html> 
  2. <head> 
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  4. <title>使用simpleXML处理XML</title> 
  5. </head> 
  6.  
  7. <body> 
  8. <table border="1" cellpadding="0" cellspacing="0" width="700"> 
  9. </tbody> 
  10. <tr bgcolor="green"> 
  11. <th width="5%">&nbsp;</th> 
  12. <th width="20%">name</th> 
  13. <th width="10%">price</th> 
  14. <th width="55%">description</th> 
  15. <th width="10%">calories</th> 
  16. </tr> 
  17. <?php 
  18.  // 使用simpleXML处理XML 
  19.  
  20.  $xml = simplexml_load_file('./simple.xml'); 
  21.  //var_dump($xml); 
  22.  //echo $xml->getName(); 
  23.  //var_dump($xml->children()); 
  24.  $record = ''; 
  25.  foreach ($xml->children() as $child) 
  26.  { 
  27.   $record .= '<tr><td>'. $child->attributes(). '</td>'; 
  28.   foreach ($child->children() as $item) 
  29.   { 
  30.    //var_dump($child); 
  31.    $record .= '<td>'. $item .'</td>'; 
  32.   } 
  33.   $record .= '</tr>'; 
  34.  }//开源代码phpfensi.com 
  35.  echo $record; 
  36. ?> 
  37. </tbody> 
  38. </table> 
  39. </body> 
  40. </html> 

将XML转换为数组结构,代码如下:

  1. private function change_simple_xml_to_array($obj){ 
  2.   $i = 0; 
  3.   //从根节点下的二层子节点开始遍历 
  4.   foreach($obj->children() as $value){ 
  5.     //如果发生同级、同标签名的情况,则加一维数组; 
  6.     if(isset($last_name) && $value->getName()==$last_name){ 
  7.       //出现同名发现第二个,则推入第二维数组; 
  8.       if($i==1) $arr[$last_name]=array($arr[$last_name]); 
  9.       //遍历子节点; 
  10.       array_push($arr[$last_name],$this->change_simple_xml_to_array($value)); 
  11.     }else{ 
  12.       $last_name = $value->getName(); 
  13.       if(count($value->children())==0) $arr[$last_name] = $value.""; 
  14.       else $arr[$last_name] = $this->change_simple_xml_to_array($value); 
  15.     } 
  16.     $i++; 
  17.   } 
  18.   return $arr; 
  19. } 
  20.  
  21. //XML文档—————— 
  22.  
  23. <?xml version="1.0" encoding="utf-8"?> 
  24. <use> 
  25.   <login id="1"> 
  26.     <user>bddg</user> 
  27.     <pas>abcdefg</pas> 
  28.     <permission><fdsa>5</fdsa></permission> 
  29.     <online_time>2</online_time> 
  30.   </login> 
  31.   <login id="2"> 
  32.     <user>baidongdaogu</user> 
  33.     <pas>aaa</pas> 
  34.     <permission>1</permission> 
  35.     <online_time>2</online_time> 
  36.   </login> 
  37.   <login id="3"> 
  38.     <user>baidongdaogu</user> 
  39.     <pas>aaa</pas> 
  40.     <permission>1</permission> 
  41.     <online_time>2</online_time> 
  42.   </login> 
  43. </use> 
  44.  
  45. //转换后的数组———— 
  46.  
  47. Array 
  48. ( 
  49.   [login] => Array 
  50.     ( 
  51.       [0] => Array 
  52.         ( 
  53.           [user] => bddg 
  54.           [pas] => abcdefg 
  55.           [permission] => Array 
  56.             ( 
  57.               [fdsa] => 5 
  58.             ) 
  59.  
  60.           [online_time] => 2 
  61.         ) 
  62.  
  63.       [1] => Array 
  64.         ( 
  65.           [user] => baidongdaogu 
  66.           [pas] => aaa 
  67.           [permission] => 1 
  68.           [online_time] => 2 
  69.         ) 
  70.  
  71.       [2] => Array 
  72.         ( 
  73.           [user] => baidongdaogu 
  74.           [pas] => aaa 
  75.           [permission] => 1 
  76.           [online_time] => 2 
  77.         ) 
  78.     ) 
  79. ) 

Tags: simplexml 使用simplexml

分享到: