|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00026 class xml_format_exception extends moodle_exception { 00028 public $errorstring; 00029 public $line; 00030 public $char; 00031 function __construct($errorstring, $line, $char, $link = '') { 00032 $this->errorstring = $errorstring; 00033 $this->line = $line; 00034 $this->char = $char; 00035 00036 $a = new stdClass(); 00037 $a->errorstring = $errorstring; 00038 $a->errorline = $line; 00039 $a->errorchar = $char; 00040 parent::__construct('errorparsingxml', 'error', $link, $a); 00041 } 00042 } 00043 00072 function xmlize($data, $whitespace = 1, $encoding = 'UTF-8', $reporterrors = false) { 00073 00074 $data = trim($data); 00075 $vals = array(); 00076 $parser = xml_parser_create($encoding); 00077 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 00078 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, $whitespace); 00079 xml_parse_into_struct($parser, $data, $vals); 00080 00081 // Error handling when the xml file is not well-formed 00082 if ($reporterrors) { 00083 $errorcode = xml_get_error_code($parser); 00084 if ($errorcode) { 00085 $exception = new xml_format_exception(xml_error_string($errorcode), 00086 xml_get_current_line_number($parser), 00087 xml_get_current_column_number($parser)); 00088 xml_parser_free($parser); 00089 throw $exception; 00090 } 00091 } 00092 xml_parser_free($parser); 00093 00094 $i = 0; 00095 if (empty($vals)) { 00096 // XML file is invalid or empty, return false 00097 return false; 00098 } 00099 00100 $array = array(); 00101 $tagname = $vals[$i]['tag']; 00102 if (isset($vals[$i]['attributes'])) { 00103 $array[$tagname]['@'] = $vals[$i]['attributes']; 00104 } else { 00105 $array[$tagname]['@'] = array(); 00106 } 00107 00108 $array[$tagname]["#"] = xml_depth($vals, $i); 00109 00110 return $array; 00111 } 00112 00119 function xml_depth($vals, &$i) { 00120 $children = array(); 00121 00122 if ( isset($vals[$i]['value']) ) 00123 { 00124 array_push($children, $vals[$i]['value']); 00125 } 00126 00127 while (++$i < count($vals)) { 00128 00129 switch ($vals[$i]['type']) { 00130 00131 case 'open': 00132 00133 if ( isset ( $vals[$i]['tag'] ) ) 00134 { 00135 $tagname = $vals[$i]['tag']; 00136 } else { 00137 $tagname = ''; 00138 } 00139 00140 if ( isset ( $children[$tagname] ) ) 00141 { 00142 $size = sizeof($children[$tagname]); 00143 } else { 00144 $size = 0; 00145 } 00146 00147 if ( isset ( $vals[$i]['attributes'] ) ) { 00148 $children[$tagname][$size]['@'] = $vals[$i]["attributes"]; 00149 00150 } 00151 00152 $children[$tagname][$size]['#'] = xml_depth($vals, $i); 00153 00154 break; 00155 00156 00157 case 'cdata': 00158 array_push($children, $vals[$i]['value']); 00159 break; 00160 00161 case 'complete': 00162 $tagname = $vals[$i]['tag']; 00163 00164 if( isset ($children[$tagname]) ) 00165 { 00166 $size = sizeof($children[$tagname]); 00167 } else { 00168 $size = 0; 00169 } 00170 00171 if( isset ( $vals[$i]['value'] ) ) 00172 { 00173 $children[$tagname][$size]["#"] = $vals[$i]['value']; 00174 } else { 00175 $children[$tagname][$size]["#"] = ''; 00176 } 00177 00178 if ( isset ($vals[$i]['attributes']) ) { 00179 $children[$tagname][$size]['@'] 00180 = $vals[$i]['attributes']; 00181 } 00182 00183 break; 00184 00185 case 'close': 00186 return $children; 00187 break; 00188 } 00189 00190 } 00191 00192 return $children; 00193 00194 00195 } 00196 00197 00214 function traverse_xmlize($array, $arrName = 'array', $level = 0) { 00215 00216 foreach($array as $key=>$val) 00217 { 00218 if ( is_array($val) ) 00219 { 00220 traverse_xmlize($val, $arrName . '[' . $key . ']', $level + 1); 00221 } else { 00222 $GLOBALS['traverse_array'][] = '$' . $arrName . '[' . $key . '] = "' . $val . "\"\n"; 00223 } 00224 } 00225 00226 return 1; 00227 00228 }