initial commit
[home-automation.git] / pxweather / xmlize-php4.inc.php
1 <?php\r
2 \r
3 /* xmlize() is by Hans Anderson, www.hansanderson.com/contact/\r
4  *\r
5  * Ye Ole "Feel Free To Use it However" License [PHP, BSD, GPL].\r
6  * some code in xml_depth is based on code written by other PHPers\r
7  * as well as one Perl script.  Poor programming practice and organization\r
8  * on my part is to blame for the credit these people aren't receiving.\r
9  * None of the code was copyrighted, though.\r
10  *\r
11  * This is a stable release, 1.0.  I don't foresee any changes, but you\r
12  * might check http://www.hansanderson.com/php/xml/ to see\r
13  *\r
14  * usage: $xml = xmlize($xml_data);\r
15  *\r
16  * See the function traverse_xmlize() for information about the\r
17  * structure of the array, it's much easier to explain by showing you.\r
18  * Be aware that the array is very complex.  I use xmlize all the time,\r
19  * but still need to use traverse_xmlize or print_r() quite often to\r
20  * show me the structure!\r
21  *\r
22  */\r
23 \r
24 function xmlize($data, $WHITE=1) {\r
25 \r
26     $data = trim($data);\r
27     $vals = $index = $array = array();\r
28     $parser = xml_parser_create();\r
29     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);\r
30     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);\r
31     if ( !xml_parse_into_struct($parser, $data, $vals, $index) )\r
32     {\r
33         die(sprintf("XML error: %s at line %d",\r
34                     xml_error_string(xml_get_error_code($parser)),\r
35                     xml_get_current_line_number($parser)));\r
36 \r
37     }\r
38     xml_parser_free($parser);\r
39 \r
40     $i = 0; \r
41 \r
42     $tagname = $vals[$i]['tag'];\r
43     if ( isset ($vals[$i]['attributes'] ) )\r
44     {\r
45         $array[$tagname]['@'] = $vals[$i]['attributes'];\r
46     } else {\r
47         $array[$tagname]['@'] = array();\r
48     }\r
49 \r
50     $array[$tagname]["#"] = xml_depth($vals, $i);\r
51 \r
52     return $array;\r
53 }\r
54 \r
55 /* \r
56  *\r
57  * You don't need to do anything with this function, it's called by\r
58  * xmlize.  It's a recursive function, calling itself as it goes deeper\r
59  * into the xml levels.  If you make any improvements, please let me know.\r
60  *\r
61  *\r
62  */\r
63 \r
64 function xml_depth($vals, &$i) { \r
65     $children = array(); \r
66 \r
67     if ( isset($vals[$i]['value']) )\r
68     {\r
69         array_push($children, $vals[$i]['value']);\r
70     }\r
71 \r
72     while (++$i < count($vals)) { \r
73 \r
74         switch ($vals[$i]['type']) { \r
75 \r
76            case 'open': \r
77 \r
78                 if ( isset ( $vals[$i]['tag'] ) )\r
79                 {\r
80                     $tagname = $vals[$i]['tag'];\r
81                 } else {\r
82                     $tagname = '';\r
83                 }\r
84 \r
85                 if ( isset ( $children[$tagname] ) )\r
86                 {\r
87                     $size = sizeof($children[$tagname]);\r
88                 } else {\r
89                     $size = 0;\r
90                 }\r
91 \r
92                 if ( isset ( $vals[$i]['attributes'] ) ) {\r
93                     $children[$tagname][$size]['@'] = $vals[$i]["attributes"];\r
94                 }\r
95 \r
96                 $children[$tagname][$size]['#'] = xml_depth($vals, $i);\r
97 \r
98             break; \r
99 \r
100 \r
101             case 'cdata':\r
102                 array_push($children, $vals[$i]['value']); \r
103             break; \r
104 \r
105             case 'complete': \r
106                 $tagname = $vals[$i]['tag'];\r
107 \r
108                 if( isset ($children[$tagname]) )\r
109                 {\r
110                     $size = sizeof($children[$tagname]);\r
111                 } else {\r
112                     $size = 0;\r
113                 }\r
114 \r
115                 if( isset ( $vals[$i]['value'] ) )\r
116                 {\r
117                     $children[$tagname][$size]["#"] = $vals[$i]['value'];\r
118                 } else {\r
119                     $children[$tagname][$size]["#"] = '';\r
120                 }\r
121 \r
122                 if ( isset ($vals[$i]['attributes']) ) {\r
123                     $children[$tagname][$size]['@']\r
124                                              = $vals[$i]['attributes'];\r
125                 }                       \r
126 \r
127             break; \r
128 \r
129             case 'close':\r
130                 return $children; \r
131             break;\r
132         } \r
133 \r
134     } \r
135 \r
136         return $children;\r
137 \r
138 }\r
139 \r
140 \r
141 /* function by acebone@f2s.com, a HUGE help!\r
142  *\r
143  * this helps you understand the structure of the array xmlize() outputs\r
144  *\r
145  * usage:\r
146  * traverse_xmlize($xml, 'xml_');\r
147  * print '<pre>' . implode("", $traverse_array . '</pre>';\r
148  *\r
149  *\r
150  */ \r
151 \r
152 function traverse_xmlize($array, $arrName = "array", $level = 0) {\r
153 \r
154     foreach($array as $key=>$val)\r
155     {\r
156         if ( is_array($val) )\r
157         {\r
158             traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);\r
159         } else {\r
160             $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";\r
161         }\r
162     }\r
163 \r
164     return 1;\r
165 \r
166 }\r
167 \r
168 ?>