* @copyright 2004-2008 Stephan Schmidt * @license http://opensource.org/licenses/bsd-license New BSD License * @version CVS: $Id$ * @link http://pear.php.net/package/XML_Parser2 */ /** * built on XML_Parser2 */ require_once 'XML/Parser2.php'; /** * Simple XML parser class. * * This class is a simplified version of XML_Parser2. * In most XML applications the real action is executed, * when a closing tag is found. * * XML_Parser2_Simple allows you to just implement one callback * for each tag that will receive the tag with its attributes * and CData. * * * require_once '../Parser/Simple.php'; * * class myParser extends XML_Parser2_Simple * { * * function handleElement($name, $attribs, $data) * { * printf('handle %s
', $name); * } * } * * $p = new myParser(); * * $result = $p->setInputFile('myDoc.xml'); * $result = $p->parse(); *
* * @category XML * @package XML_Parser2 * @author Stephan Schmidt * @copyright 2004-2008 The PHP Group * @license http://opensource.org/licenses/bsd-license New BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/XML_Parser2 */ class XML_Parser2_Simple extends XML_Parser2 { /** * element stack * * @access private * @var array */ var $_elStack = array(); /** * all character data * * @access private * @var array */ var $_data = array(); /** * element depth * * @access private * @var integer */ var $_depth = 0; /** * Mapping from expat handler function to class method. * * @var array */ var $handler = array( 'default_handler' => 'defaultHandler', 'processing_instruction_handler' => 'piHandler', 'unparsed_entity_decl_handler' => 'unparsedHandler', 'notation_decl_handler' => 'notationHandler', 'external_entity_ref_handler' => 'entityrefHandler' ); /** * inits the handlers * * @return mixed * @access private */ function _initHandlers() { if (!is_object($this->_handlerObj)) { $this->_handlerObj = $this; } if ($this->mode != 'func' && $this->mode != 'event') { throw new XML_Parser2_Exception('Unsupported mode given', XML_PARSER2_ERROR_UNSUPPORTED_MODE); } xml_set_object($this->parser, $this->_handlerObj); xml_set_element_handler($this->parser, array($this, 'startHandler'), array($this, 'endHandler')); xml_set_character_data_handler($this->parser, array($this, 'cdataHandler')); /** * set additional handlers for character data, entities, etc. */ foreach ($this->handler as $xml_func => $method) { if (method_exists($this->_handlerObj, $method)) { $xml_func = 'xml_set_' . $xml_func; $xml_func($this->parser, $method); } } } /** * Reset the parser. * * This allows you to use one parser instance * to parse multiple XML documents. * * @access public * @return boolean|object true on success, PEAR_Error otherwise */ function reset() { $this->_elStack = array(); $this->_data = array(); $this->_depth = 0; $this->_create(); return true; } /** * start handler * * Pushes attributes and tagname onto a stack * * @param resource $xp xml parser resource * @param string $elem element name * @param array $attribs attributes * * @return mixed * @access private * @final */ function startHandler($xp, $elem, $attribs) { array_push($this->_elStack, array( 'name' => $elem, 'attribs' => $attribs )); $this->_depth++; $this->_data[$this->_depth] = ''; } /** * end handler * * Pulls attributes and tagname from a stack * * @param resource $xp xml parser resource * @param string $elem element name * * @return mixed * @access private * @final */ function endHandler($xp, $elem) { $el = array_pop($this->_elStack); $data = $this->_data[$this->_depth]; $this->_depth--; switch ($this->mode) { case 'event': $this->_handlerObj->handleElement($el['name'], $el['attribs'], $data); break; case 'func': $func = 'handleElement_' . $elem; if (strchr($func, '.')) { $func = str_replace('.', '_', $func); } if (method_exists($this->_handlerObj, $func)) { call_user_func(array($this->_handlerObj, $func), $el['name'], $el['attribs'], $data); } break; } } /** * handle character data * * @param resource $xp xml parser resource * @param string $data data * * @return void * @access private * @final */ function cdataHandler($xp, $data) { $this->_data[$this->_depth] .= $data; } /** * handle a tag * * Implement this in your parser * * @param string $name element name * @param array $attribs attributes * @param string $data character data * * @return void * @access public * @abstract */ function handleElement($name, $attribs, $data) { } /** * get the current tag depth * * The root tag is in depth 0. * * @access public * @return integer */ function getCurrentDepth() { return $this->_depth; } /** * add some string to the current ddata. * * This is commonly needed, when a document is parsed recursively. * * @param string $data data to add * * @return void * @access public */ function addToData($data) { $this->_data[$this->_depth] .= $data; } } ?>