updated removed
[home-automation.git] / xml2array.php
1 <?
2 function xml2array($contents, $get_attributes=1, $priority = 'tag') {
3     if(!$contents) return array();
4
5     if(!function_exists('xml_parser_create')) {
6         //print "'xml_parser_create()' function not found!";
7         return array();
8     }
9
10     //Get the XML parser of PHP - PHP must have this module for the parser to work
11     $parser = xml_parser_create('');
12     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
13     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
14     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
15     xml_parse_into_struct($parser, trim($contents), $xml_values);
16     xml_parser_free($parser);
17
18     if(!$xml_values) return;//Hmm...
19
20     //Initializations
21     $xml_array = array();
22     $parents = array();
23     $opened_tags = array();
24     $arr = array();
25
26     $current = &$xml_array; //Refference
27
28     //Go through the tags.
29     $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
30     foreach($xml_values as $data) {
31         unset($attributes,$value);//Remove existing values, or there will be trouble
32
33         //This command will extract these variables into the foreach scope
34         // tag(string), type(string), level(int), attributes(array).
35         extract($data);//We could use the array by itself, but this cooler.
36
37         $result = array();
38         $attributes_data = array();
39         
40         if(isset($value)) {
41             if($priority == 'tag') $result = $value;
42             else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
43         }
44
45         //Set the attributes too.
46         if(isset($attributes) and $get_attributes) {
47             foreach($attributes as $attr => $val) {
48                 if($priority == 'tag') $attributes_data[$attr] = $val;
49                 else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
50             }
51         }
52
53         //See tag status and do the needed.
54         if($type == "open") {//The starting of the tag '<tag>'
55             $parent[$level-1] = &$current;
56             if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
57                 $current[$tag] = $result;
58                 if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
59                 $repeated_tag_index[$tag.'_'.$level] = 1;
60
61                 $current = &$current[$tag];
62
63             } else { //There was another element with the same tag name
64
65                 if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
66                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
67                     $repeated_tag_index[$tag.'_'.$level]++;
68                 } else {//This section will make the value an array if multiple tags with the same name appear together
69                     $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
70                     $repeated_tag_index[$tag.'_'.$level] = 2;
71                     
72                     if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
73                         $current[$tag]['0_attr'] = $current[$tag.'_attr'];
74                         unset($current[$tag.'_attr']);
75                     }
76
77                 }
78                 $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
79                 $current = &$current[$tag][$last_item_index];
80             }
81
82         } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
83             //See if the key is already taken.
84             if(!isset($current[$tag])) { //New Key
85                 $current[$tag] = $result;
86                 $repeated_tag_index[$tag.'_'.$level] = 1;
87                 if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
88
89             } else { //If taken, put all things inside a list(array)
90                 if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
91
92                     // ...push the new element into that array.
93                     $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
94                     
95                     if($priority == 'tag' and $get_attributes and $attributes_data) {
96                         $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
97                     }
98                     $repeated_tag_index[$tag.'_'.$level]++;
99
100                 } else { //If it is not an array...
101                     $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
102                     $repeated_tag_index[$tag.'_'.$level] = 1;
103                     if($priority == 'tag' and $get_attributes) {
104                         if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
105                             
106                             $current[$tag]['0_attr'] = $current[$tag.'_attr'];
107                             unset($current[$tag.'_attr']);
108                         }
109                         
110                         if($attributes_data) {
111                             $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
112                         }
113                     }
114                     $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
115                 }
116             }
117
118         } elseif($type == 'close') { //End of tag '</tag>'
119             $current = &$parent[$level-1];
120         }
121     }
122     
123     return($xml_array);
124 }  
125 ?>