From b73c57b42890f6ea7f6494bec25e4199056c86d2 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 22 Jan 2005 05:25:15 +0000 Subject: [PATCH] XML parser now should (in theory) be working. --- xml.php | 122 +++++++++++++++++++++++++------------------------------- 1 file changed, 54 insertions(+), 68 deletions(-) diff --git a/xml.php b/xml.php index 42e5aa2..58b85ae 100644 --- a/xml.php +++ b/xml.php @@ -58,12 +58,24 @@ class XML_Parser */ 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); @@ -74,9 +86,47 @@ class XML_Parser exit; } + // destroy the parser xml_parser_free($this->parser); - return array(); + // 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 . '"', ERR_FATAL); + } + } + + $return["$tagid"] = array('tagid' => $tagid, 'tagname' => $tagname, 'parentid' => $parentid, 'attributes' => $this->attrdata["$tagid"], 'data' => $data); + } + } + } + + // done... send the results back + return $return; } /** @@ -122,28 +172,15 @@ class XML_Parser */ function _cdata($parser, $data) { - global $_isso; - static $count; - - /*if (preg_replace('#(^[[:space:]]+|[[:space:]]+$)#', '', trim($data)) == '') - { - $count++; - $datanew = str_replace(array("\t", "\n", "\r", " "), array("{t}", "{n}", "{r}", "{s}"), $data); - $_isso->debug("strangechars[" . $this->tagid . "] = '$datanew'"); - $data = $datanew . "||$count||"; - //return; - }*/ - - $_isso->debug("cdata[] = '$data'"); // read in the CDATA if ($this->parent == $this->tagid) { - if (!isset($this->taginfo[ $this->tagname ][ $this->parent - 1 ][ $this->tagid ])) + if (!isset($this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ])) { - $this->taginfo[ $this->tagname ][ $this->parent - 1 ][ $this->tagid ] = ''; + $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] = ''; } - $this->taginfo[ $this->tagname ][ $this->parent - 1 ][ $this->tagid ] .= $data; + $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] .= $data; } else { @@ -159,57 +196,6 @@ class XML_Parser */ function _tag_end($parser, $name) { - global $_isso; - - // fetch the data - if (isset($this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ])) - { - $data = $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ]; - } - else - { - return; - } - - if (trim($data) == '') - { - $_isso->debug("not going to bother with '" . $this->tagid . "'"); - } - - // if we have a data handler, operate it now - if (isset($this->taghandler[ $this->tagname ])) - { - $_isso->debug("handler: {$this->taghandler[ " . $this->tagname . " ]}"); - if (function_exists($this->taghandler[ $this->tagname ])) - { - $data = $this->taghandler[ $this->tagname ]($data); - } - } - - // check for necessary trims - if ($this->trimdata) - { - // trim() doesn't take off the edge for more than a few white characters, preg_replace does; don't use [:space:] either... - //$data = preg_replace('#(^\s+|\s+$)#', '', $data); - //$data = preg_replace('#(^[[:space:]]+|[[:space:]]+$)#', '', trim($data)); - $data = preg_replace('#^\s+#', '', $data); - $data = preg_replace('#\s+$#', '', $data); - $_isso->debug("trimming data ('" . $this->tagid . "'): '" . $data . "'"); - //$data = trim($data); - } - - // don't want blank fields - if (trim($data) != '') - { - $this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ] = $data; - } - else - { - unset($this->taginfo[ $this->tagname ][ $this->parent ][ $this->tagid ]); - } - - $_isso->debug("tree[] = " . current($this->tree)); - // make sure that we're always at the end of the tree end($this->tree); } -- 2.43.5