📄 parser.php
字号:
<?php//// +----------------------------------------------------------------------+// | PHP Version 4 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2004 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 3.0 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available at through the world-wide-web at |// | http://www.php.net/license/3_0.txt. |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Author: Stig Bakken <ssb@fast.no> |// | Tomas V.V.Cox <cox@idecnet.com> |// | Stephan Schmidt <schst@php-tools.net> |// +----------------------------------------------------------------------+//// $Id: Parser.php,v 1.26 2005/09/23 11:51:10 schst Exp $/** * XML Parser class. * * This is an XML parser based on PHP's "xml" extension, * based on the bundled expat library. * * @category XML * @package XML_Parser * @author Stig Bakken <ssb@fast.no> * @author Tomas V.V.Cox <cox@idecnet.com> * @author Stephan Schmidt <schst@php-tools.net> *//** * uses PEAR's error handling */require_once 'PEAR.php';/** * resource could not be created */define('XML_PARSER_ERROR_NO_RESOURCE', 200);/** * unsupported mode */define('XML_PARSER_ERROR_UNSUPPORTED_MODE', 201);/** * invalid encoding was given */define('XML_PARSER_ERROR_INVALID_ENCODING', 202);/** * specified file could not be read */define('XML_PARSER_ERROR_FILE_NOT_READABLE', 203);/** * invalid input */define('XML_PARSER_ERROR_INVALID_INPUT', 204);/** * remote file cannot be retrieved in safe mode */define('XML_PARSER_ERROR_REMOTE', 205);/** * XML Parser class. * * This is an XML parser based on PHP's "xml" extension, * based on the bundled expat library. * * Notes: * - It requires PHP 4.0.4pl1 or greater * - From revision 1.17, the function names used by the 'func' mode * are in the format "xmltag_$elem", for example: use "xmltag_name" * to handle the <name></name> tags of your xml file. * * @category XML * @package XML_Parser * @author Stig Bakken <ssb@fast.no> * @author Tomas V.V.Cox <cox@idecnet.com> * @author Stephan Schmidt <schst@php-tools.net> * @todo create XML_Parser_Namespace to parse documents with namespaces * @todo create XML_Parser_Pull * @todo Tests that need to be made: * - mixing character encodings * - a test using all expat handlers * - options (folding, output charset) * - different parsing modes */class XML_Parser extends PEAR{ // {{{ properties /** * XML parser handle * * @var resource * @see xml_parser_create() */ var $parser; /** * File handle if parsing from a file * * @var resource */ var $fp; /** * Whether to do case folding * * If set to true, all tag and attribute names will * be converted to UPPER CASE. * * @var boolean */ var $folding = true; /** * Mode of operation, one of "event" or "func" * * @var string */ var $mode; /** * Mapping from expat handler function to class method. * * @var array */ var $handler = array( 'character_data_handler' => 'cdataHandler', 'default_handler' => 'defaultHandler', 'processing_instruction_handler' => 'piHandler', 'unparsed_entity_decl_handler' => 'unparsedHandler', 'notation_decl_handler' => 'notationHandler', 'external_entity_ref_handler' => 'entityrefHandler' ); /** * source encoding * * @var string */ var $srcenc; /** * target encoding * * @var string */ var $tgtenc; /** * handler object * * @var object */ var $_handlerObj; // }}} // {{{ constructor /** * Creates an XML parser. * * This is needed for PHP4 compatibility, it will * call the constructor, when a new instance is created. * * @param string $srcenc source charset encoding, use NULL (default) to use * whatever the document specifies * @param string $mode how this parser object should work, "event" for * startelement/endelement-type events, "func" * to have it call functions named after elements * @param string $tgenc a valid target encoding */ function XML_Parser($srcenc = null, $mode = 'event', $tgtenc = null) { XML_Parser::__construct($srcenc, $mode, $tgtenc); } // }}} /** * PHP5 constructor * * @param string $srcenc source charset encoding, use NULL (default) to use * whatever the document specifies * @param string $mode how this parser object should work, "event" for * startelement/endelement-type events, "func" * to have it call functions named after elements * @param string $tgenc a valid target encoding */ function __construct($srcenc = null, $mode = 'event', $tgtenc = null) { $this->PEAR('XML_Parser_Error'); $this->mode = $mode; $this->srcenc = $srcenc; $this->tgtenc = $tgtenc; } // }}} /** * Sets the mode of the parser. * * Possible modes are: * - func * - event * * You can set the mode using the second parameter * in the constructor. * * This method is only needed, when switching to a new * mode at a later point. * * @access public * @param string mode, either 'func' or 'event' * @return boolean|object true on success, PEAR_Error otherwise */ function setMode($mode) { if ($mode != 'func' && $mode != 'event') { $this->raiseError('Unsupported mode given', XML_PARSER_ERROR_UNSUPPORTED_MODE); } $this->mode = $mode; return true; } /** * Sets the object, that will handle the XML events * * This allows you to create a handler object independent of the * parser object that you are using and easily switch the underlying * parser. * * If no object will be set, XML_Parser assumes that you * extend this class and handle the events in $this. * * @access public * @param object object to handle the events * @return boolean will always return true * @since v1.2.0beta3 */ function setHandlerObj(&$obj) { $this->_handlerObj = &$obj; return true; } /** * Init the element handlers * * @access private */ function _initHandlers() { if (!is_resource($this->parser)) { return false; } if (!is_object($this->_handlerObj)) { $this->_handlerObj = &$this; } switch ($this->mode) { case 'func': xml_set_object($this->parser, $this->_handlerObj); xml_set_element_handler($this->parser, array(&$this, 'funcStartHandler'), array(&$this, 'funcEndHandler')); break; case 'event': xml_set_object($this->parser, $this->_handlerObj); xml_set_element_handler($this->parser, 'startHandler', 'endHandler'); break; default: return $this->raiseError('Unsupported mode given', XML_PARSER_ERROR_UNSUPPORTED_MODE); break; } /** * 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); } } } // {{{ _create() /** * create the XML parser resource * * Has been moved from the constructor to avoid * problems with object references. * * Furthermore it allows us returning an error * if something fails. * * @access private * @return boolean|object true on success, PEAR_Error otherwise * * @see xml_parser_create */ function _create() { if ($this->srcenc === null) { $xp = @xml_parser_create(); } else { $xp = @xml_parser_create($this->srcenc); } if (is_resource($xp)) { if ($this->tgtenc !== null) { if (!@xml_parser_set_option($xp, XML_OPTION_TARGET_ENCODING, $this->tgtenc)) { return $this->raiseError('invalid target encoding', XML_PARSER_ERROR_INVALID_ENCODING); } } $this->parser = $xp; $result = $this->_initHandlers($this->mode); if ($this->isError($result)) { return $result; } xml_parser_set_option($xp, XML_OPTION_CASE_FOLDING, $this->folding); return true; } return $this->raiseError('Unable to create XML parser resource.', XML_PARSER_ERROR_NO_RESOURCE); } // }}} // {{{ reset()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -