parser = xml_parser_create(); $this->document = array(); $this->currTag = &$this->document; } function parse($xml) { $this->current = &$this->document; xml_set_object($this->parser, $this); xml_set_character_data_handler($this->parser, 'dataHandler'); xml_set_element_handler($this->parser, 'startHandler', 'endHandler'); if(!xml_parse($this->parser, $xml, 1)) { print sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser)); return false; } xml_parser_free($this->parser); return true; } function startHandler($parser, $name, $attribs) { // create array for the new <$name> tag to live in $new_tag = array(); // give this tag a reference to its parent tag $new_tag["tag-parent"] = &$this->current; // let this tag remember its name $new_tag["tag-name"] = $name; // add this new tag to the current tag if (!isset($this->current[$name])) { $this->current[$name] = array(); } $this->current[$name][] = &$new_tag; // make the current tag the new tag $this->current = &$new_tag; } function dataHandler($parser, $data) { $data = trim($data); if ($data=="") { return; } // add data to the current tag if (!isset($this->current["data"])) { $this->current["data"] = ""; } $this->current["data"].=$data; } function endHandler($parser, $name) { // tag has been closed, check we closed the right one, if ($this->current["tag-name"]!=$name) { print "Bad XML"; exit(); } $temp = &$this->current["tag-parent"]; // clean up temps unset($this->current["tag-parent"]); unset($this->current["tag-name"]); // make the current tag the parent of the current tag $this->current = &$temp; } } ?>