php专区

 首页 > php专区 > PHP应用 > 常用功能 > PHP simplexml使用方法详解 - php高级应用

PHP simplexml使用方法详解 - php高级应用

分享到:
【字体:大 中 小】
导读:
          本文章给来大家讲一下关于PHP下simplexml使用方法,希望通过此文章能提升大家对simplexml操作xml文档的理解,有需要了解的朋友可参考.PHP XML处理XML代码如下:?xmlversion=1.0encoding=UTF-8?......

PHP simplexml使用方法详解

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

PHP XML处理XML代码如下:

  1. xml version="1.0" encoding="UTF-8"?> 
  2.  
  3. <breakfast_menu> 
  4.  <food id="1"> 
  5.   <name>Belgian Wafflesname> 
  6.   <price>$5.95price> 
  7.   <description>two of our famous Belgian Waffles with plenty of real maple syrupdescription> 
  8.   <calories>650calories> 
  9.  food> 
  10.  <food id="2"> 
  11.   <name>Strawberry Belgian Wafflesname> 
  12.   <price>$7.95price> 
  13.   <description>light Belgian waffles covered with strawberries and whipped creamdescription> 
  14.   <calories>900calories> 
  15.  food> 
  16.  <food id="3"> 
  17.   <name>Berry-Berry Belgian Wafflesname> 
  18.   <price>$8.95price> 
  19.   <description>light Belgian waffles covered with an assortment of fresh berries and whipped creamdescription> 
  20.   <calories>900calories> 
  21.  food> 
  22. breakfast_menu> 

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

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

将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. "1.0" encoding="utf-8"?> 
  24. <use> 
  25.   "1"> 
  26.     bddg 
  27.     abcdefg 
  28.     5 
  29.     2 
  30.    
  31.   "2"> 
  32.     baidongdaogu 
  33.     aaa 
  34.     1 
  35.     2 
  36.    
  37.   "3"> 
  38.     baidongdaogu 
  39.     aaa 
  40.     1 
  41.     2 
  42.    
  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. ) 
分享到:
php计划任务示例详解 - php高级应用
php计划任务示例详解 一、在Crontab中使用PHP执行脚本  就像在Crontab中调用普通的shell脚本一样,具体Crontab用法,使用PHP程序来调用PHP脚本,每一小时执行myscript.php如下: #crontab -e   00 * * * * /usr/local/bin/php /home/web/abc.php/usr/local/bin/  php为P...
PHP生成和获取XML格式数据实现代码 - php...
PHP生成和获取XML格式数据实现代码 本文章来给大家介绍在php中生成和获取XML格式数据代码,生成xml我们使用DOMDocument,读取xml我们使用XMLReader即可,下面我分别给大家介绍. 生成XML格式数据 我们假设系统中有一张学生信息表student,需要提供给第三方调用,并有id,name,sex,age分别记录学...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……