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. //XML文档—————— 
  21.  
  22. "1.0" encoding="utf-8"?> 
  23. <use
  24.   "1"
  25.     bddg 
  26.     abcdefg 
  27.     5 
  28.     2 
  29.    
  30.   "2"
  31.     baidongdaogu 
  32.     aaa 
  33.     1 
  34.     2 
  35.    
  36.   "3"
  37.     baidongdaogu 
  38.     aaa 
  39.     1 
  40.     2 
  41.    
  42. use
  43.  
  44. //转换后的数组———— 
  45.  
  46. Array 
  47.   [login] => Array 
  48.     ( 
  49.       [0] => Array 
  50.         ( 
  51.           [user] => bddg 
  52.           [pas] => abcdefg 
  53.           [permission] => Array 
  54.             ( 
  55.               [fdsa] => 5 
  56.             ) 
  57.  
  58.           [online_time] => 2 
  59.         ) 
  60.  
  61.       [1] => Array 
  62.         ( 
  63.           [user] => baidongdaogu 
  64.           [pas] => aaa 
  65.           [permission] => 1 
  66.           [online_time] => 2 
  67.         ) 
  68.  
  69.       [2] => Array 
  70.         ( 
  71.           [user] => baidongdaogu 
  72.           [pas] => aaa 
  73.           [permission] => 1 
  74.           [online_time] => 2 
  75.         ) 
  76.     ) 
分享到:
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技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……