·您当前的位置:首页 > 技术教程 > PHP教程 >

[php]php的xml文件读取XMLReader源代码范例

时间:2017-01-08 10:11cnblogs.com
[php]php的xml文件读取XMLReader源代码范例

[php]php的xml文件读取XMLReader,[php]php的xml文件读取XMLReader源代码范例

  1. <?php 
  2. /** 
  3. $xmlString = '<xml> 
  4.         <persons count="10"> 
  5.             <person username="username1" age="20">this is username1 description</person> 
  6.             <person username="username2" age="20">this is username2 description</person> 
  7.         </persons> 
  8.     </xml>'; 
  9.      
  10. $mXmlTest = new XmlTest(); 
  11. $dataList = $mXmlTest->parseXmlStr($xmlString); 
  12. print_r($dataList); 
  13.  
  14. $xmlFile = './test.xml'
  15. $dataList = $mXmlTest->parseXmlFile($xmlFile); 
  16. print_r($dataList); 
  17.  
  18.  */ 
  19.  
  20.  
  21. class XmlTest{ 
  22.          
  23. //     <xml> 
  24. //         <persons count="10"> 
  25. //             <person username="username1" age="20">this is username1 description</person> 
  26. //             <person username="username2" age="20">this is username2 description</person> 
  27. //         </persons> 
  28. //     </xml> 
  29.      
  30.     /** 
  31.      * 解析返回的XML文档 
  32.      */ 
  33.     public function parseXmlFile($xmlFile){ 
  34.         $reader = new \XMLReader(); 
  35.         $reader->open($xmlFile, 'UTF-8'); 
  36.         $nodeName = ''
  37.         $dataList = array(); 
  38.         $data = array(); 
  39.         while ($reader->read()){ 
  40.             if($reader->nodeType == \XMLReader::ELEMENT){ 
  41.                 $nodeName = $reader->name; 
  42.                 if($nodeName=='persons'){ 
  43.                     $count = $reader->getAttribute('count'); 
  44.                     if(!($count>0)){ 
  45.                         break; 
  46.                     } 
  47.                 } 
  48.                 elseif($nodeName=='person'){ 
  49.                     $data = array
  50.                         'username'=>$reader->getAttribute('username'), 
  51.                         'age'=>$reader->getAttribute('age'), 
  52.                     ); 
  53.                 } 
  54.             } 
  55.             if($reader->nodeType == \XMLReader::TEXT && !empty($nodeName)){ 
  56.                 if($nodeName=='person'){ 
  57.                     $data['description'] = strtolower($reader->value); 
  58.                     $dataList[] = $data; 
  59.                 } 
  60.             } 
  61.         } 
  62.         $reader->close(); 
  63.         return $dataList; 
  64.     } 
  65.      
  66.     /** 
  67.      * 解析返回的XML文档 
  68.      */ 
  69.     public function parseXmlStr($xmlString){ 
  70.         $reader = new \XMLReader(); 
  71.         $reader->xml($xmlString,'UTF-8'); 
  72.         $nodeName = ''
  73.         $dataList = array(); 
  74.         $data = array(); 
  75.         while ($reader->read()){ 
  76.             if($reader->nodeType == \XMLReader::ELEMENT){ 
  77.                 $nodeName = $reader->name; 
  78.                 if($nodeName=='persons'){ 
  79.                     $count = $reader->getAttribute('count'); 
  80.                     if(!($count>0)){ 
  81.                         break; 
  82.                     } 
  83.                 } 
  84.                 elseif($nodeName=='person'){ 
  85.                     $data = array
  86.                         'username'=>$reader->getAttribute('username'), 
  87.                         'age'=>$reader->getAttribute('age'), 
  88.                     ); 
  89.                 } 
  90.             } 
  91.             if($reader->nodeType == \XMLReader::TEXT && !empty($nodeName)){ 
  92.                 if($nodeName=='person'){ 
  93.                     $data['description'] = strtolower($reader->value); 
  94.                     $dataList[] = $data; 
  95.                 } 
  96.             } 
  97.         } 
  98.         return $dataList; 
  99.     } 

 来源:http://www.cnblogs.com/xiaoyaogege/p/6212161.html

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器