initial commit
[home-automation.git] / pxweather / xmlize-php5.inc.php
1 <?php\r
2 \r
3 /* xmlize() is by Hans Anderson, me@hansanderson.com\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($array);\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 somewhat tricky.  I use xmlize all the time,\r
19  * but still need to use traverse_xmlize quite often to show me the structure!\r
20 \r
21  ## THIS IS A PHP 5 VERSION:\r
22 \r
23         > attached is the modified script. Basically it has a new optional parameter\r
24         > to specify an OUTPUT encoding. If not specified, it defaults to UTF-8.\r
25         > I recommend you to read this PHP bug. There you can see how PHP4, PHP5.0.0\r
26         > and PHP5.0.2 will handle this.\r
27         > http://bugs.php.net/bug.php?id=29711\r
28         > Ciao, Eloy :-)\r
29  ##\r
30  *\r
31  */\r
32 \r
33 function xmlize($data, $WHITE=1, $encoding='UTF-8') {\r
34 \r
35     $data = trim($data);\r
36     $vals = $index = $array = array();\r
37     $parser = xml_parser_create($encoding);\r
38     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);\r
39     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);\r
40     xml_parse_into_struct($parser, $data, $vals, $index);\r
41     xml_parser_free($parser);\r
42 \r
43     $i = 0;\r
44 \r
45     $tagname = $vals[$i]['tag'];\r
46     if ( isset ($vals[$i]['attributes'] ) )\r
47     {\r
48         $array[$tagname]['@'] = $vals[$i]['attributes'];\r
49     } else {\r
50         $array[$tagname]['@'] = array();\r
51     }\r
52 \r
53     $array[$tagname]["#"] = xml_depth($vals, $i);\r
54 \r
55 \r
56     return $array;\r
57 }\r
58 \r
59 /*\r
60  *\r
61  * You don't need to do anything with this function, it's called by\r
62  * xmlize.  It's a recursive function, calling itself as it goes deeper\r
63  * into the xml levels.  If you make any improvements, please let me know.\r
64  *\r
65  *\r
66  */\r
67 \r
68 function xml_depth($vals, &$i) {\r
69     $children = array();\r
70 \r
71     if ( isset($vals[$i]['value']) )\r
72     {\r
73         array_push($children, $vals[$i]['value']);\r
74     }\r
75 \r
76     while (++$i < count($vals)) {\r
77 \r
78         switch ($vals[$i]['type']) {\r
79 \r
80            case 'open':\r
81 \r
82                 if ( isset ( $vals[$i]['tag'] ) )\r
83                 {\r
84                     $tagname = $vals[$i]['tag'];\r
85                 } else {\r
86                     $tagname = '';\r
87                 }\r
88 \r
89                 if ( isset ( $children[$tagname] ) )\r
90                 {\r
91                     $size = sizeof($children[$tagname]);\r
92                 } else {\r
93                     $size = 0;\r
94                 }\r
95 \r
96                 if ( isset ( $vals[$i]['attributes'] ) ) {\r
97                     $children[$tagname][$size]['@'] = $vals[$i]["attributes"];\r
98 \r
99                 }\r
100 \r
101                 $children[$tagname][$size]['#'] = xml_depth($vals, $i);\r
102 \r
103             break;\r
104 \r
105 \r
106             case 'cdata':\r
107                 array_push($children, $vals[$i]['value']);\r
108             break;\r
109 \r
110             case 'complete':\r
111                 $tagname = $vals[$i]['tag'];\r
112 \r
113                 if( isset ($children[$tagname]) )\r
114                 {\r
115                     $size = sizeof($children[$tagname]);\r
116                 } else {\r
117                     $size = 0;\r
118                 }\r
119 \r
120                 if( isset ( $vals[$i]['value'] ) )\r
121                 {\r
122                     $children[$tagname][$size]["#"] = $vals[$i]['value'];\r
123                 } else {\r
124                     $children[$tagname][$size]["#"] = '';\r
125                 }\r
126 \r
127                 if ( isset ($vals[$i]['attributes']) ) {\r
128                     $children[$tagname][$size]['@']\r
129                                              = $vals[$i]['attributes'];\r
130                 }\r
131 \r
132             break;\r
133 \r
134             case 'close':\r
135                 return $children;\r
136             break;\r
137         }\r
138 \r
139     }\r
140 \r
141         return $children;\r
142 \r
143 \r
144 }\r
145 \r
146 \r
147 /* function by acebone@f2s.com, a HUGE help!\r
148  *\r
149  * this helps you understand the structure of the array xmlize() outputs\r
150  *\r
151  * usage:\r
152  * traverse_xmlize($xml, 'xml_');\r
153  * print '<pre>' . implode("", $traverse_array . '</pre>';\r
154  *\r
155  *\r
156  */\r
157 \r
158 function traverse_xmlize($array, $arrName = "array", $level = 0) {\r
159 \r
160     foreach($array as $key=>$val)\r
161     {\r
162         if ( is_array($val) )\r
163         {\r
164             traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);\r
165         } else {\r
166             $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";\r
167         }\r
168     }\r
169 \r
170     return 1;\r
171 \r
172 }\r
173 \r
174 ?>