| 1 | <? |
| 2 | /* |
| 3 | * XML Array class - parses an XML file and inserts the parsed data into a |
| 4 | * multidimensional array. |
| 5 | * |
| 6 | * Version 1.2 - 2001-03-02 - Chris Bolt - chris at bolt dot cx |
| 7 | * |
| 8 | * Simple Usage: $xmlfile = new xmlarray(); |
| 9 | * $arr = $xmlfile->parsefile("somefile.xml"); |
| 10 | * |
| 11 | * More detailed documentation can be found at |
| 12 | * http://www.bolt.cx/apps/xmlarray/xmlarray.html |
| 13 | */ |
| 14 | |
| 15 | // for html decoding |
| 16 | include_once("string.inc"); |
| 17 | |
| 18 | class xmlarray { |
| 19 | |
| 20 | var $parser; |
| 21 | var $data = array(), $indexes = array(); |
| 22 | var $indexidx = -1; |
| 23 | var $position; |
| 24 | var $idname; |
| 25 | var $combine, $combined = false; |
| 26 | var $xmlerrorcode, $xmlerrorline; |
| 27 | var $useincpath, $rootarray; |
| 28 | var $newelement; |
| 29 | |
| 30 | function startElement($parser, $name, $attribs) { |
| 31 | $this->newelement = true; |
| 32 | $this->indexidx++; |
| 33 | //($this->indexes[$this->indexidx]["name"] != $name && ! $this->indexes[$this->indexidx]["name"]) ? $this->indexes[$this->indexidx] = array("count" => 0, "name" => $name) : $this->indexes[$this->indexidx]["count"]++; |
| 34 | if (empty($this->indexes[$this->indexidx]['name']) && isset($name) ) { |
| 35 | $this->indexes[$this->indexidx] = array("count" => 0, "name" => $name); |
| 36 | } else { |
| 37 | $this->indexes[$this->indexidx]["count"]++; |
| 38 | } |
| 39 | if (empty($this->position) ) { |
| 40 | $this->position=""; |
| 41 | } |
| 42 | $this->position .= "[\"$name\"][" . (isset($this->indexes[$this->indexidx]["count"]) ? $this->indexes[$this->indexidx]["count"] : 0 ) . "]"; |
| 43 | |
| 44 | if (sizeof($attribs)) { |
| 45 | $atts = array(); |
| 46 | while (list($k, $v) = each($attribs)) |
| 47 | $atts[$k] = $v; |
| 48 | eval("\$this->data$this->position = \$atts;"); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | function endElement($parser, $name) { |
| 53 | unset($this->indexes[$this->indexidx + 1]); |
| 54 | $this->indexidx--; |
| 55 | |
| 56 | // for empty elements; too much sofisticated:) |
| 57 | $pos = "\$this->data{$this->position}"; |
| 58 | $value = @eval("return $pos;"); |
| 59 | if (empty($value) ) { |
| 60 | eval("$pos = '';"); |
| 61 | } |
| 62 | |
| 63 | for ($i = 0; $i < 2; $i++) |
| 64 | $this->position = substr($this->position, 0, strrpos($this->position, "[")); |
| 65 | } |
| 66 | |
| 67 | function characterData($parser, $data) { |
| 68 | // replace parse_nl to |
| 69 | $data = str_replace("#parse_nl#"," ",$data); |
| 70 | // unhtml |
| 71 | $data = String::unhtmlspecialchars($data); |
| 72 | |
| 73 | // position for eval |
| 74 | $pos = "\$this->data$this->position"; |
| 75 | |
| 76 | // only non blank element will be parsed |
| 77 | if (trim($data)) { |
| 78 | $code = "if (count($pos) < 1) |
| 79 | $pos = \$data; |
| 80 | elseif (\$this->newelement == false) |
| 81 | $pos .= \$data; |
| 82 | elseif (count($pos) == 1) { |
| 83 | \$tmp = array($pos, \$data); |
| 84 | $pos = \$tmp; |
| 85 | } else |
| 86 | $pos" . "[] = \$data;"; |
| 87 | @eval($code); |
| 88 | } |
| 89 | $this->newelement = false; |
| 90 | } |
| 91 | |
| 92 | function walkarray(&$array) { |
| 93 | if (!is_array($array)) return $array; |
| 94 | reset($array); |
| 95 | while (list($key, $value) = each($array)) { |
| 96 | if (is_array($array[$key])) { |
| 97 | if (count($array[$key]) == 1) |
| 98 | $array[$key] = $array[$key][key($array[$key])]; |
| 99 | if (is_array($array[$key])) { |
| 100 | $array[$key] = $this->walkarray($array[$key]); |
| 101 | // if (!empty($this->idname) && !empty($array[$key][$this->idname]) && is_int($key)) { |
| 102 | if (($this->idname != "") && (($array[$key][$this->idname] != "") || is_int($array[$key][$this->idname])) && is_int($key)) { |
| 103 | if (ereg("[^0-9]", $array[$key][$this->idname])) |
| 104 | $array[$array[$key][$this->idname]] = $array[$key]; |
| 105 | else |
| 106 | $array["$this->idname" . "_" . $array[$key][$this->idname]] = $array[$key]; |
| 107 | unset($array[$key]); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | return $array; |
| 113 | } |
| 114 | |
| 115 | function xmlarray($idname = "", $rootarray = "", $combinesinglearrays = true, $useincpath = false) { |
| 116 | $this->idname = $idname; |
| 117 | $this->rootarray = $rootarray; |
| 118 | $this->combine = $combinesinglearrays; |
| 119 | $this->useincpath = $useincpath; |
| 120 | } |
| 121 | |
| 122 | function parsefile($filename) { |
| 123 | $numargs = func_num_args(); |
| 124 | if ($numargs > 1) { |
| 125 | $funcarg = func_get_arg(0); |
| 126 | $temparray = $this->parsefile($funcarg); |
| 127 | for ($i = 1; $i < $numargs; $i++) { |
| 128 | $funcarg = func_get_arg($i); |
| 129 | $temparray = array_merge_recursive($temparray, $this->parsefile($funcarg)); |
| 130 | } |
| 131 | return $temparray; |
| 132 | } |
| 133 | else |
| 134 | $filename = func_get_arg(0); |
| 135 | |
| 136 | $this->data = array(); |
| 137 | $this->indexes = array(); |
| 138 | $this->indexidx = -1; |
| 139 | unset($this->position); |
| 140 | $this->combined = false; |
| 141 | |
| 142 | $this->parser = xml_parser_create(); |
| 143 | xml_set_object($this->parser, $this); |
| 144 | xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); |
| 145 | xml_set_element_handler($this->parser, "startElement", "endElement"); |
| 146 | xml_set_character_data_handler($this->parser, "characterData"); |
| 147 | |
| 148 | $fp = fopen($filename, "r", $this->useincpath); |
| 149 | if ($fp) { |
| 150 | while ($tmpdata = fread($fp, 4096)) |
| 151 | if (!xml_parse($this->parser, $tmpdata, feof($fp))) { |
| 152 | $this->xmlerrorcode = xml_get_error_code($this->parser); |
| 153 | $this->xmlerrorline = xml_get_current_line_number($this->parser); |
| 154 | xml_parser_free($this->parser); |
| 155 | fclose($fp); |
| 156 | return false; |
| 157 | } |
| 158 | fclose($fp); |
| 159 | } |
| 160 | |
| 161 | if ($this->combine == true) { |
| 162 | $this->data = $this->walkarray($this->data); |
| 163 | $this->combined = true; |
| 164 | } |
| 165 | xml_parser_free($this->parser); |
| 166 | |
| 167 | return ($this->rootarray != "") ? $this->data[$this->rootarray] : $this->data; |
| 168 | } |
| 169 | |
| 170 | function parse($data) { |
| 171 | // fix to \n proper parsing |
| 172 | // \n is replaced by #parse_nl# |
| 173 | // back replace is in characterData function |
| 174 | $data = str_replace(" ","#parse_nl#",$data); |
| 175 | |
| 176 | $this->data = array(); |
| 177 | $this->indexes = array(); |
| 178 | $this->indexidx = -1; |
| 179 | unset($this->position); |
| 180 | $this->combined = false; |
| 181 | |
| 182 | $this->parser = xml_parser_create(); |
| 183 | xml_set_object($this->parser, $this); |
| 184 | xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); |
| 185 | xml_set_element_handler($this->parser, "startElement", "endElement"); |
| 186 | xml_set_character_data_handler($this->parser, "characterData"); |
| 187 | |
| 188 | if (!xml_parse($this->parser, $data,1)) { |
| 189 | $this->xmlerrorcode = xml_error_string(xml_get_error_code($this->parser)); |
| 190 | $this->xmlerrorline = xml_get_current_line_number($this->parser); |
| 191 | echo $this->xmlerrorcode."<br>"; |
| 192 | echo $this->xmlerrorline; |
| 193 | xml_parser_free($this->parser); |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | if ($this->combine == true) { |
| 198 | $this->data = $this->walkarray($this->data); |
| 199 | $this->combined = true; |
| 200 | } |
| 201 | xml_parser_free($this->parser); |
| 202 | |
| 203 | return ($this->rootarray != "") ? $this->data[$this->rootarray] : $this->data; |
| 204 | } |
| 205 | |
| 206 | } |
| 207 | ?> |