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