Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / lib / xmlize.php
blob34f29dfcf1cf67f87fa04b09c00495fbed850cde
1 <?php
3 /**
4 * xmlize.php - xmlize() is by Hans Anderson, {@link http://www.hansanderson.com/contact/}
6 * Ye Ole "Feel Free To Use it However" License [PHP, BSD, GPL].
7 * some code in xml_depth is based on code written by other PHPers
8 * as well as one Perl script. Poor programming practice and organization
9 * on my part is to blame for the credit these people aren't receiving.
10 * None of the code was copyrighted, though.
12 * @author Hans Anderson
13 * @version This is a stable release, 1.0. I don't foresee any changes, but you
14 * might check {@link http://www.hansanderson.com/php/xml/} to see
15 * @package moodlecore
18 /**
19 * Create xml formatted output from an array.
21 * usage:<br>
22 * <code>
23 * $xml = xmlize($array);
24 * </code>
25 * See the function {@link traverse_xmlize()} for information about the
26 * structure of the array, it's much easier to explain by showing you.
27 * Be aware that the array is somewhat tricky. I use xmlize all the time,
28 * but still need to use {@link traverse_xmlize()} quite often to show me the structure!
30 * THIS IS A PHP 5 VERSION:
32 * This modified version basically has a new optional parameter
33 * to specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
34 * I recommend you to read this PHP bug. There you can see how PHP4, PHP5.0.0
35 * and PHP5.0.2 will handle this.
36 * {@link http://bugs.php.net/bug.php?id=29711}
37 * Ciao, Eloy :-)
40 * @author Hans Anderson
41 * @param array $data The array to be converted
42 * @param int $WHITE If set to 1 allows the parser to skip "space" characters in xml document. Default is 0
43 * @param string $encoding Specify an OUTPUT encoding. If not specified, it defaults to UTF-8.
44 * @return array
46 function xmlize($data, $WHITE=1, $encoding='UTF-8') {
48 $data = trim($data);
49 $vals = $index = $array = array();
50 $parser = xml_parser_create($encoding);
51 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
52 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $WHITE);
53 xml_parse_into_struct($parser, $data, $vals, $index);
54 xml_parser_free($parser);
56 $i = 0;
58 if (empty($vals)) {
59 // XML file is invalid or empty, return false
60 return false;
63 $tagname = $vals[$i]['tag'];
64 if ( isset ($vals[$i]['attributes'] ) )
66 $array[$tagname]['@'] = $vals[$i]['attributes'];
67 } else {
68 $array[$tagname]['@'] = array();
71 $array[$tagname]["#"] = xml_depth($vals, $i);
74 return $array;
77 /**
78 * @internal You don't need to do anything with this function, it's called by
79 * xmlize. It's a recursive function, calling itself as it goes deeper
80 * into the xml levels. If you make any improvements, please let me know.
81 * @access private
83 function xml_depth($vals, &$i) {
84 $children = array();
86 if ( isset($vals[$i]['value']) )
88 array_push($children, $vals[$i]['value']);
91 while (++$i < count($vals)) {
93 switch ($vals[$i]['type']) {
95 case 'open':
97 if ( isset ( $vals[$i]['tag'] ) )
99 $tagname = $vals[$i]['tag'];
100 } else {
101 $tagname = '';
104 if ( isset ( $children[$tagname] ) )
106 $size = sizeof($children[$tagname]);
107 } else {
108 $size = 0;
111 if ( isset ( $vals[$i]['attributes'] ) ) {
112 $children[$tagname][$size]['@'] = $vals[$i]["attributes"];
116 $children[$tagname][$size]['#'] = xml_depth($vals, $i);
118 break;
121 case 'cdata':
122 array_push($children, $vals[$i]['value']);
123 break;
125 case 'complete':
126 $tagname = $vals[$i]['tag'];
128 if( isset ($children[$tagname]) )
130 $size = sizeof($children[$tagname]);
131 } else {
132 $size = 0;
135 if( isset ( $vals[$i]['value'] ) )
137 $children[$tagname][$size]["#"] = $vals[$i]['value'];
138 } else {
139 $children[$tagname][$size]["#"] = '';
142 if ( isset ($vals[$i]['attributes']) ) {
143 $children[$tagname][$size]['@']
144 = $vals[$i]['attributes'];
147 break;
149 case 'close':
150 return $children;
151 break;
156 return $children;
163 * This helps you understand the structure of the array {@link xmlize()} outputs
165 * Function by acebone@f2s.com, a HUGE help!<br>
166 * Usage:<br>
167 * <code>
168 * traverse_xmlize($xml, 'xml_');
169 * print '<pre>' . implode("", $traverse_array . '</pre>';
170 * </code>
171 * @author acebone@f2s.com
172 * @param array $array ?
173 * @param string $arrName ?
174 * @param int $level ?
175 * @return int
176 * @todo Finish documenting this function
178 function traverse_xmlize($array, $arrName = 'array', $level = 0) {
180 foreach($array as $key=>$val)
182 if ( is_array($val) )
184 traverse_xmlize($val, $arrName . '[' . $key . ']', $level + 1);
185 } else {
186 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n";
190 return 1;