当前位置:首页 > PHP教程 > php函数 > 列表

php xml转换成数组

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-20 15:18:04 浏览: 评论:0 
  1. //调用方法: 
  2. $array =  xml2array(file_get_contents('feed.xml', 1, 'attribute')); 
  3. */ 
  4. function xml2array($contents, $get_attributes=1, $priority = 'tag') { 
  5.     if(!$contents) return array(); 
  6.  
  7.     if(!function_exists('xml_parser_create')) { 
  8.         //print "'xml_parser_create()' function not found!"; 
  9.         return array(); 
  10.     } 
  11.  
  12.     //get the xml parser of php - php must have this module for the parser to work 
  13.     $parser = xml_parser_create(''); 
  14.     xml_parser_set_option($parser, xml_option_target_encoding, "utf-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss 
  15.     xml_parser_set_option($parser, xml_option_case_folding, 0); 
  16.     xml_parser_set_option($parser, xml_option_skip_white, 1); 
  17.     xml_parse_into_struct($parser, trim($contents), $xml_values); 
  18.     xml_parser_free($parser); 
  19.  
  20.     if(!$xml_values) return;//hmm... 
  21.  
  22.     //initializations 
  23.     $xml_array = array(); 
  24.     $parents = array(); 
  25.     $opened_tags = array(); 
  26.     $arr = array(); 
  27.  
  28.     $current = &$xml_array; //refference 
  29.  
  30.     //go through the tags. 
  31.     $repeated_tag_index = array();//multiple tags with same name will be turned into an array 
  32.     foreach($xml_values as $data) { 
  33.         unset($attributes,$value);//remove existing values, or there will be trouble 
  34.  
  35.         //this command will extract these variables into the foreach scope 
  36.         // tag(string), type(string), level(int), attributes(array). 
  37.         extract($data);//we could use the array by itself, but this cooler. 
  38.  
  39.         $result = array(); 
  40.         $attributes_data = array(); 
  41.  
  42.         if(isset($value)) { 
  43.             if($priority == 'tag') $result = $value; 
  44.             else $result['value'] = $value; //put the value in a assoc array if we are in the 'attribute' mode 
  45.         } 
  46.  
  47.         //set the attributes too. 
  48.         if(isset($attributes) and $get_attributes) { 
  49.             foreach($attributes as $attr => $val) { 
  50.                 if($priority == 'tag') $attributes_data[$attr] = $val; 
  51.                 else $result['attr'][$attr] = $val; //set all the attributes in a array called 'attr' 
  52.             } 
  53.         } 
  54.  
  55.         //see tag status and do the needed. 
  56.         if($type == "open") {//the starting of the tag '<tag>' 
  57.             $parent[$level-1] = &$current; 
  58.             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //insert new tag 
  59.                 $current[$tag] = $result; 
  60.                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data; 
  61.                 $repeated_tag_index[$tag.'_'.$level] = 1; 
  62.  
  63.                 $current = &$current[$tag]; 
  64.  
  65.             } else { //there was another element with the same tag name 
  66.  
  67.                 if(isset($current[$tag][0])) {//if there is a 0th element it is already an array 
  68.                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
  69.                     $repeated_tag_index[$tag.'_'.$level]++; 
  70.                 } else {//this section will make the value an array if multiple tags with the same name appear together 
  71.                     $current[$tag] = array($current[$tag],$result);//this will combine the existing item and the new item together to make an array 
  72.                     $repeated_tag_index[$tag.'_'.$level] = 2; 
  73.  
  74.                     if(isset($current[$tag.'_attr'])) { //the attribute of the last(0th) tag must be moved as well 
  75.                         $current[$tag]['0_attr'] = $current[$tag.'_attr']; 
  76.                         unset($current[$tag.'_attr']); 
  77.                     } 
  78.  
  79.                 } 
  80.                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1; 
  81.                 $current = &$current[$tag][$last_item_index]; 
  82.             } 
  83.  
  84.         } elseif($type == "complete") { //tags that ends in 1 line '<tag />' 
  85.             //see if the key is already taken. 
  86.             if(!isset($current[$tag])) { //new key 
  87.                 $current[$tag] = $result; 
  88.                 $repeated_tag_index[$tag.'_'.$level] = 1; 
  89.                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data; 
  90.  
  91.             } else { //if taken, put all things inside a list(array) 
  92.                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//if it is already an array... 
  93.  
  94.                     // ...push the new element into that array. 
  95.                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result; 
  96.  
  97.                     if($priority == 'tag' and $get_attributes and $attributes_data) { 
  98.                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
  99.                     } 
  100.                     $repeated_tag_index[$tag.'_'.$level]++; 
  101.  
  102.                 } else { //if it is not an array... 
  103.                     $current[$tag] = array($current[$tag],$result); //...make it an array using using the existing value and the new value 
  104.                     $repeated_tag_index[$tag.'_'.$level] = 1; 
  105.                     if($priority == 'tag' and $get_attributes) { 
  106.                         if(isset($current[$tag.'_attr'])) { //the attribute of the last(0th) tag must be moved as well 
  107.  
  108.                             $current[$tag]['0_attr'] = $current[$tag.'_attr']; 
  109.                             unset($current[$tag.'_attr']); 
  110.                         } 
  111.  
  112.                         if($attributes_data) { 
  113.                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data; 
  114.                         } 
  115.                     } 
  116.                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken 
  117.                 } 
  118.             } 
  119.  
  120.         } elseif($type == 'close') { //end of tag '</tag>' 
  121.             $current = &$parent[$level-1]; 
  122.         }//开源代码phpfensi.com 
  123.     } 
  124.  
  125.     return($xml_array); 
  126. } 
  127.  
  128. ######################################################################################################### 
  129.  
  130. class parserxml 
  131. { 
  132.   function parserxml() 
  133.   { 
  134.  
  135.   } 
  136.   function parserxmlbyurl($url) 
  137.   { 
  138.      $fp = @fopen($url,'r'); 
  139.   if (!$fp){ 
  140.     return 1; 
  141.   } 
  142.   $xmlstr = fread($fp,1024); 
  143.   @fclose($fp); 
  144.   if ($xmlstr === false){ 
  145.     return 0; 
  146.   } 
  147.   return $this->getxmltree($xmlstr); 
  148.   } 
  149.   function getxmltree($xmldata) 
  150.   { 
  151.     ini_set ('track_errors', '1'); 
  152.     $xmlreaderror = false; 
  153.     $parser = xml_parser_create(); 
  154.     xml_parser_set_option ($parser, xml_option_skip_white, 1); 
  155.     xml_parser_set_option ($parser, xml_option_case_folding, 0); 
  156.     if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) { 
  157.        $xmlreaderror = true; 
  158.        return 0; 
  159.     } 
  160.     xml_parser_free($parser); 
  161.  
  162.     if(!$xmlreaderror){ 
  163.        $result = array (); 
  164.        $i = 0; 
  165.        if (isset($vals[$i]['attributes'])){ 
  166.            foreach (array_keys ($vals [$i]['attributes']) as $attkey) 
  167.            $attributes[$attkey] = $vals[$i]['attributes'][$attkey]; 
  168.     } 
  169.        $result[$vals [$i]['tag']] = array_merge((array)$attributes, $this->getchildren($vals, $i, 'open')); 
  170.     } 
  171.     ini_set('track_errors', '0'); 
  172.     return $result; 
  173.   } 
  174.   function getchildren ($vals, &$i, $type) 
  175.   { 
  176.     if ($type == 'complete') { 
  177.        if (isset ($vals [$i]['value'])) 
  178.            return ($vals [$i]['value']); 
  179.        else 
  180.            return ''; 
  181.   } 
  182.   $children = array (); 
  183.   while ($vals [++$i]['type'] != 'close') { 
  184.        $type = $vals [$i]['type']; 
  185.        if (isset ($children [$vals [$i]['tag']])) { 
  186.            if (is_array ($children [$vals [$i]['tag']])) { 
  187.                $temp = array_keys ($children [$vals [$i]['tag']]); 
  188.                if (is_string ($temp [0])) { 
  189.                    $a = $children [$vals [$i]['tag']]; 
  190.                    unset ($children [$vals [$i]['tag']]); 
  191.                    $children [$vals [$i]['tag']][0] = $a; 
  192.                } 
  193.            } else { 
  194.                $a = $children [$vals [$i]['tag']]; 
  195.                unset ($children [$vals [$i]['tag']]); 
  196.                $children [$vals [$i]['tag']][0] = $a; 
  197.            } 
  198.  
  199.            $children [$vals [$i]['tag']][] = $this->getchildren ($vals, $i, $type); 
  200.        } else 
  201.            $children [$vals [$i]['tag']] = $this->getchildren ($vals, $i, $type); 
  202.        if (isset ($vals [$i]['attributes'])) { 
  203.            $attributes = array (); 
  204.            foreach (array_keys ($vals [$i]['attributes']) as $attkey) 
  205.            $attributes [$attkey] = $vals [$i]['attributes'][$attkey]; 
  206.            if (isset ($children [$vals [$i]['tag']])) { 
  207.                if ($children [$vals [$i]['tag']] == '') { 
  208.                    unset ($children [$vals [$i]['tag']]); 
  209.                    $children [$vals [$i]['tag']] = $attributes; 
  210.                } 
  211.                elseif (is_array ($children [$vals [$i]['tag']])) { 
  212.                    $index = count ($children [$vals [$i]['tag']]) - 1; 
  213.                    if ($children [$vals [$i]['tag']][$index] == '') { 
  214.                        unset ($children [$vals [$i]['tag']][$index]); 
  215.                        $children [$vals [$i]['tag']][$index] = $attributes; 
  216.                    } 
  217.                    $children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes); 
  218.                } else { 
  219.                    $value = $children [$vals [$i]['tag']]; 
  220.                    unset ($children [$vals [$i]['tag']]); 
  221.                    $children [$vals [$i]['tag']]['value'] = $value; 
  222.                    $children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes); 
  223.                } 
  224.            } else 
  225.                $children [$vals [$i]['tag']] = $attributes; 
  226.        } 
  227.   } 
  228.     return $children; 
  229. ?> 

Tags: php xml转换成数组

分享到: