From a0cbfcd6bb82bbe764fe7a61f906f2a49834223a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 11 Nov 2006 23:07:51 +0000 Subject: [PATCH] Updated the XML parsing framework module --- xml.php | 121 ++++++++++++++------------------------------------------ 1 file changed, 30 insertions(+), 91 deletions(-) diff --git a/xml.php b/xml.php index b3b6ea5..21066f6 100644 --- a/xml.php +++ b/xml.php @@ -20,8 +20,7 @@ \*=====================================================================*/ /** -* XML Parser -* xml.php +* Static XML parser (XML.php) * * @package ISSO */ @@ -29,7 +28,8 @@ /** * XML Parser * -* This framework is a wrapper for a robust XML parser. +* A simple XML parser that is run by calling BSXml::Parse( String xmlData) and an +* array is returned with the parsed information. * * @author Blue Static * @copyright Copyright ©2002 - [#]year[#], Blue Static @@ -37,14 +37,8 @@ * @package ISSO * */ -class XML +class BSXml { - /** - * Framework registry object - * @var object - */ - private $registry = null; - /** * Parser resource * @var integer @@ -81,47 +75,11 @@ class XML */ private $result = array(); - /** - * Fields array that is used in this module - * @var array - */ - private $fields = array( - 'taghandler' => array(REQ_NO, null, false) - ); - // ################################################################### /** * Constructor */ - public function __construct(&$registry) - { - $this->registry =& $registry; - } - - // ################################################################### - /** - * Sets an ISSO field - * - * @param string Field name - * @param mixed Value of the field - */ - public function set($name, $value) - { - $this->registry->do_set($name, $value, 'xml'); - } - - // ################################################################### - /** - * Gets an ISSO field - * - * @param string Field name - * - * @return mixed Value of the field - */ - public function get($fieldname) - { - return $this->registry->do_get($fieldname, 'xml'); - } + private function __construct() {} // ################################################################### /** @@ -132,49 +90,44 @@ class XML * * @return array Array with all the XML data parsed */ - public function parse($data, $utf8 = false) + public static function Parse($data, $utf8 = false) { - $this->registry->check_isso_fields(get_class($this)); - - $this->stack = array(); - $this->attribs = array(); - $this->result = array(); - $this->cdata = ''; - + $parser = new BSXml(); + if ($utf8) { $data = utf8_encode($data); - $this->parser = xml_parser_create('UTF-8'); + $parser->parser = xml_parser_create('UTF-8'); } else { - $this->parser = xml_parser_create('ISO-8859-1'); + $parser->parser = xml_parser_create('ISO-8859-1'); } // create a new parser - xml_set_object($this->parser, $this); - xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); - xml_set_element_handler($this->parser, 'handle_tag_start', 'handle_tag_end'); - xml_set_character_data_handler($this->parser, 'handle_cdata'); + xml_set_object($parser->parser, $parser); + xml_parser_set_option($parser->parser, XML_OPTION_CASE_FOLDING, 0); + xml_set_element_handler($parser->parser, '_handleStartTag', '_handleEndTag'); + xml_set_character_data_handler($parser->parser, '_handleCData'); - $this->attach_node($this->result); + $parser->_attachNode($parser->result); // parse the data and check for errors - if (!xml_parse($this->parser, $data)) + if (!xml_parse($parser->parser, $data)) { - $error['code'] = xml_get_error_code($this->parser); + $error['code'] = xml_get_error_code($parser->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); + $error['line'] = xml_get_current_line_number($parser->parser); + $error['column'] = xml_get_current_column_number($parser->parser); trigger_error("XML Error: $error[string] at line $error[line] colunn $error[column]"); exit; } // destroy the parser - xml_parser_free($this->parser); + xml_parser_free($parser->parser); // done... send the results back - return $this->result; + return $parser->result; } // ################################################################### @@ -185,7 +138,7 @@ class XML * @param string Tag name * @param array Tag attributes */ - private function handle_tag_start(&$parser, $name, $attrs) + private function _handleStartTag(&$parser, $name, $attrs) { // we need to keep track of indicies to monitor the last key in $this->attribs static $index; @@ -206,14 +159,14 @@ class XML // create a new child node $this->attribs["$name"][ $index["$name"] ] = (array)$attrs; - $this->attach_node($this->attribs["$name"][ $index["$name"] ]); + $this->_attachNode($this->attribs["$name"][ $index["$name"] ]); $index["$name"]++; } // node set doesn't exist, so create it else { $this->attribs["$name"] = (array)$attrs; - $this->attach_node($this->attribs["$name"]); + $this->_attachNode($this->attribs["$name"]); $index["$name"] = 1; } } @@ -225,7 +178,7 @@ class XML * @param integer XML parser * @param string CDATA from tag */ - private function handle_cdata(&$parser, $data) + private function _handleCData(&$parser, $data) { $this->cdata .= $data; } @@ -237,32 +190,18 @@ class XML * @param integer XML parser * @param string Tag name */ - private function handle_tag_end(&$parser, $name) + private function _handleEndTag(&$parser, $name) { // attach data to the node if (($this->cdata = trim($this->cdata)) != '') { - // if we have a data handler, operate it now - if (isset($this->taghandler["$name"])) - { - $this->registry->debug("handler: " . $this->taghandler["$name"]); - if (function_exists($this->taghandler["$name"])) - { - $this->cdata = $this->taghandler["$name"]($this->cdata, $this); - } - else - { - trigger_error('Could not find the function [' . $this->taghandler["$name"] . '()] for the XML tag "' . $name . '"'); - } - } - $this->attribs['value'] = $this->cdata; } $this->cdata = ''; // remove the node - $this->detach_node(); + $this->_detachNode(); } // ################################################################### @@ -271,7 +210,7 @@ class XML * * @param array Node to place into the stack */ - private function attach_node(&$node) + private function _attachNode(&$node) { // create a new node $this->stack[ sizeof($this->stack) ] =& $node; @@ -284,7 +223,7 @@ class XML /** * Unshifts the node tree */ - private function detach_node() + private function _detachNode() { // drop the newest node unset($this->stack[ sizeof($this->stack) - 1 ]); @@ -304,7 +243,7 @@ class XML * * @param array The node to int-index */ - public function unify_node(&$node) + public static function UnifyNode(&$node) { if (!isset($node[0])) { -- 2.22.5