Fleshed out WebCT dox a bit
[moodle.git] / lib / xmlize.php
blob3bffc6a039787e685e26c19026708abfc1e8be74
1 <?php
3 /* xmlize() is by Hans Anderson, me@hansanderson.com
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.
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
14 * usage: $xml = xmlize($array);
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!
23 function xmlize($data, $WHITE=1) {
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);
33 $i = 0;
35 $tagname = $vals[$i]['tag'];
36 if ( isset ($vals[$i]['attributes'] ) )
38 $array[$tagname]['@'] = $vals[$i]['attributes'];
39 } else {
40 $array[$tagname]['@'] = array();
43 $array[$tagname]["#"] = xml_depth($vals, $i);
46 return $array;
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.
58 function xml_depth($vals, &$i) {
59 $children = array();
61 if ( isset($vals[$i]['value']) )
63 array_push($children, $vals[$i]['value']);
66 while (++$i < count($vals)) {
68 switch ($vals[$i]['type']) {
70 case 'open':
72 if ( isset ( $vals[$i]['tag'] ) )
74 $tagname = $vals[$i]['tag'];
75 } else {
76 $tagname = '';
79 if ( isset ( $children[$tagname] ) )
81 $size = sizeof($children[$tagname]);
82 } else {
83 $size = 0;
86 if ( isset ( $vals[$i]['attributes'] ) ) {
87 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
91 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
93 break;
96 case 'cdata':
97 array_push($children, $vals[$i]['value']);
98 break;
100 case 'complete':
101 $tagname = $vals[$i]['tag'];
103 if( isset ($children[$tagname]) )
105 $size = sizeof($children[$tagname]);
106 } else {
107 $size = 0;
110 if( isset ( $vals[$i]['value'] ) )
112 $children[$tagname][$size]["#"] = $vals[$i]['value'];
113 } else {
114 $children[$tagname][$size]["#"] = '';
117 if ( isset ($vals[$i]['attributes']) ) {
118 $children[$tagname][$size]['@']
119 = $vals[$i]['attributes'];
122 break;
124 case 'close':
125 return $children;
126 break;
131 return $children;
137 /* function by acebone@f2s.com, a HUGE help!
139 * this helps you understand the structure of the array xmlize() outputs
141 * usage:
142 * traverse_xmlize($xml, 'xml_');
143 * print '<pre>' . implode("", $traverse_array . '</pre>';
148 function traverse_xmlize($array, $arrName = "array", $level = 0) {
150 foreach($array as $key=>$val)
152 if ( is_array($val) )
154 traverse_xmlize($val, $arrName . "[" . $key . "]", $level + 1);
155 } else {
156 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
160 return 1;