Useful function for XML ... currently used only in Blackboard quiz importing
[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!
20 *
21 */
22
23function xmlize($data, $WHITE=1) {
24
25 $data = trim($data);
26 $vals = $index = $array = array();
27 $parser = xml_parser_create();
28 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
29 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
30 xml_parse_into_struct($parser, $data, $vals, $index);
31 xml_parser_free($parser);
32
33 $i = 0;
34
35 $tagname = $vals[$i]['tag'];
36 if ( isset ($vals[$i]['attributes'] ) )
37 {
38 $array[$tagname]['@'] = $vals[$i]['attributes'];
39 } else {
40 $array[$tagname]['@'] = array();
41 }
42
43 $array[$tagname]["#"] = xml_depth($vals, $i);
44
45
46 return $array;
47}
48
49/*
50 *
51 * You don't need to do anything with this function, it's called by
52 * xmlize. It's a recursive function, calling itself as it goes deeper
53 * into the xml levels. If you make any improvements, please let me know.
54 *
55 *
56 */
57
58function xml_depth($vals, &$i) {
59 $children = array();
60
61 if ( isset($vals[$i]['value']) )
62 {
63 array_push($children, $vals[$i]['value']);
64 }
65
66 while (++$i < count($vals)) {
67
68 switch ($vals[$i]['type']) {
69
70 case 'open':
71
72 if ( isset ( $vals[$i]['tag'] ) )
73 {
74 $tagname = $vals[$i]['tag'];
75 } else {
76 $tagname = '';
77 }
78
79 if ( isset ( $children[$tagname] ) )
80 {
81 $size = sizeof($children[$tagname]);
82 } else {
83 $size = 0;
84 }
85
86 if ( isset ( $vals[$i]['attributes'] ) ) {
87 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
88
89 }
90
91 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
92
93 break;
94
95
96 case 'cdata':
97 array_push($children, $vals[$i]['value']);
98 break;
99
100 case 'complete':
101 $tagname = $vals[$i]['tag'];
102
103 if( isset ($children[$tagname]) )
104 {
105 $size = sizeof($children[$tagname]);
106 } else {
107 $size = 0;
108 }
109
110 if( isset ( $vals[$i]['value'] ) )
111 {
112 $children[$tagname][$size]["#"] = $vals[$i]['value'];
113 } else {
114 $children[$tagname][$size]["#"] = '';
115 }
116
117 if ( isset ($vals[$i]['attributes']) ) {
118 $children[$tagname][$size]['@']
119 = $vals[$i]['attributes'];
120 }
121
122 break;
123
124 case 'close':
125 return $children;
126 break;
127 }
128
129 }
130
131 return $children;
132
133
134}
135
136
137/* function by acebone@f2s.com, a HUGE help!
138 *
139 * this helps you understand the structure of the array xmlize() outputs
140 *
141 * usage:
142 * traverse_xmlize($xml, 'xml_');
143 * print '<pre>' . implode("", $traverse_array . '</pre>';
144 *
145 *
146 */
147
148function traverse_xmlize($array, $arrName = "array", $level = 0) {
149
150 foreach($array as $key=>$val)
151 {
152 if ( is_array($val) )
153 {
154 traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
155 } else {
156 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
157 }
158 }
159
160 return 1;
161
162}
163
164?>
165