taginfo = array(); $this->attrdata = array(); $this->tagid = -1; $this->tagname = ''; $this->tree = array(); $this->parentid = -1; // create a new parser $this->parser = xml_parser_create(); xml_set_object($this->parser, $this); xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); xml_set_element_handler($this->parser, '_tag_start', '_tag_end'); xml_set_character_data_handler($this->parser, '_cdata'); // parse the data and check for errors if (!xml_parse($this->parser, $data)) { $error['code'] = xml_get_error_code($this->parser); $error['string'] = xml_error_string($error['code']); $error['line'] = xml_get_current_line_number($this->parser); $error['column'] = xml_get_current_column_number($this->parser); print(sprintf("XML Error: %s (%d) at line %d colunn %d", $error['string'], $error['code'], $error['line'], $error['column'])); exit; } // destroy the parser xml_parser_free($this->parser); // data manipulation -- kind of resource intensive :-/ foreach ($this->taginfo AS $tagname => $parents) { foreach ($parents AS $parentid => $ids) { foreach ($ids AS $tagid => $data) { // trim data if necessary if ($this->trimdata) { $_isso->debug("data trim: on"); if (($trimmed = trim($data)) != '') { $data = $trimmed; } } // if we have a data handler, operate it now if (isset($this->taghandler["$tagname"])) { $_isso->debug("handler: " . $this->taghandler["$tagname"]); if (function_exists($this->taghandler["$tagname"])) { $data = $this->taghandler["$tagname"]($data, $this); } else { trigger_error('Could not find the function [' . $this->taghandler["$tagname"] . '()] for the XML tag "' . $tagname . '"', E_USER_ERROR); } } $return["$tagid"] = array('tagid' => $tagid, 'tagname' => $tagname, 'parentid' => $parentid, 'attributes' => $this->attrdata["$tagid"], 'data' => $data); } } } // done... send the results back return $return; } /** * Process the opening location of an XML tag * * @param res XML parser * @param str Tag name * @param array Tag attributes */ function _tag_start($parser, $name, $attributes) { // we're opening a new tag $this->tagid++; $this->tagname = $name; // copy all tag attributes array_walk($attributes, 'trim'); $this->attrdata[ $this->tagid ] = $attributes; $this->parent = current($this->tree); if ($this->parent === false) { $this->parent = -1; } // advance the parent counter because the tree is yet to be updated else { $this->parent++; } // initialize the data set $this->taginfo["$name"][ $this->parent ][ $this->tagid ] = ''; // add the current tag into the tree $this->tree[] = $this->tagid; } /** * Process XML CDATA * * @param res XML parser * @param str CDATA from tag */ function _cdata($parser, $data) { // read in the CDATA if ($this->parent == $this->tagid) { if (!isset($this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ])) { $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] = ''; } $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] .= $data; } else { $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] .= $data; } } /** * Process the closing of an XML tag * * @param res XML parser * @param str Tag name */ function _tag_end($parser, $name) { // make sure that we're always at the end of the tree end($this->tree); } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>