function) * @var array() * @see parse() */ var $taghandler = array(); /** * Whether we should trim CDATA elements * @var bool * @see parse() */ var $trimdata = true; /** * Attribute data parsed from the tags * @var array * @see _tag_start() */ var $attrdata = array(); /** * Internal tracker for the current tag * @var int; */ var $tagid = -1; /** * Internal tracker for the name of the current tag * @var str */ var $tagname = ''; /** * An internal tree to sort out the nesting of elements * @var array */ var $tree = array(); /** * Internal tracker for the tag above the current one * @var int */ var $parent = -1; /** * Parse an XML file * * @param str XML file data * * @return array Array with all the XML data parsed */ function parse($data) { global $_isso; // reset all the parser data before starting $this->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); } } } // place the normal order on things ksort($return); // 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; // refocus the array end($this->tree); // calculate the parent $this->parent = current($this->tree); $this->parent = (($this->parent === false) ? -1 : $this->parent); // add the current element onto the array array_push($this->tree, $this->tagid); // initialize the data set $this->taginfo["$name"][ $this->parent ][ $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 AND !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; } /** * Process the closing of an XML tag * * @param res XML parser * @param str Tag name */ function _tag_end($parser, $name) { // pop off the end element array_pop($this->tree); } } /*=====================================================================*\ || ################################################################### || # $HeadURL$ || # $Id$ || ################################################################### \*=====================================================================*/ ?>