3 * XML Array class - parses an XML file and inserts the parsed data into a
4 * multidimensional array.
6 * Version 1.2 - 2001-03-02 - Chris Bolt - chris at bolt dot cx
8 * Simple Usage: $xmlfile = new xmlarray();
9 * $arr = $xmlfile->parsefile("somefile.xml");
11 * More detailed documentation can be found at
12 * http://www.bolt.cx/apps/xmlarray/xmlarray.html
16 include_once("string.inc");
21 var $data = array(), $indexes = array();
25 var $combine, $combined = false;
26 var $xmlerrorcode, $xmlerrorline;
27 var $useincpath, $rootarray;
30 function startElement($parser, $name, $attribs) {
31 $this->newelement = true;
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);
37 $this->indexes[$this->indexidx]["count"]++;
39 if (empty($this->position) ) {
42 $this->position .= "[\"$name\"][" . (isset($this->indexes[$this->indexidx]["count"]) ? $this->indexes[$this->indexidx]["count"] : 0 ) . "]";
44 if (sizeof($attribs)) {
46 while (list($k, $v) = each($attribs))
48 eval("\$this->data$this->position = \$atts;");
52 function endElement($parser, $name) {
53 unset($this->indexes[$this->indexidx + 1]);
56 // for empty elements; too much sofisticated:)
57 $pos = "\$this->data{$this->position}";
58 $value = @eval("return $pos;");
63 for ($i = 0; $i < 2; $i++)
64 $this->position = substr($this->position, 0, strrpos($this->position, "["));
67 function characterData($parser, $data) {
68 // replace parse_nl to
69 $data = str_replace("#parse_nl#"," ",$data);
71 $data = String::unhtmlspecialchars($data);
74 $pos = "\$this->data$this->position";
76 // only non blank element will be parsed
78 $code = "if (count($pos) < 1)
80 elseif (\$this->newelement == false)
82 elseif (count($pos) == 1) {
83 \$tmp = array($pos, \$data);
86 $pos" . "[] = \$data;";
89 $this->newelement = false;
92 function walkarray(&$array) {
93 if (!is_array($array)) return $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];
106 $array["$this->idname" . "_" . $array[$key][$this->idname]] = $array[$key];
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;
122 function parsefile($filename) {
123 $numargs = func_num_args();
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));
134 $filename = func_get_arg(0);
136 $this->data = array();
137 $this->indexes = array();
138 $this->indexidx = -1;
139 unset($this->position);
140 $this->combined = false;
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");
148 $fp = fopen($filename, "r", $this->useincpath);
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);
161 if ($this->combine == true) {
162 $this->data = $this->walkarray($this->data);
163 $this->combined = true;
165 xml_parser_free($this->parser);
167 return ($this->rootarray != "") ? $this->data[$this->rootarray] : $this->data;
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);
176 $this->data = array();
177 $this->indexes = array();
178 $this->indexidx = -1;
179 unset($this->position);
180 $this->combined = false;
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");
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);
197 if ($this->combine == true) {
198 $this->data = $this->walkarray($this->data);
199 $this->combined = true;
201 xml_parser_free($this->parser);
203 return ($this->rootarray != "") ? $this->data[$this->rootarray] : $this->data;