Implements a simple XML parser base class that can be overridden to create an XML parser.
Implements a simple XML parser base class that can be overridden to create an XML parser.
You should extend this class and add methods of the form start_foo($attr) and end_foo(), where foo is the tagname you want to match.
You can also add methods of the form handle_foo_data($data) to handle character data that appears in the tag named foo. This can be used to easily extract data from simple XML documents.
If the parser encounters a tag that does not have corresponding start_foo and end_foo methods, unknown_starttag or unknown_endtag are called.
Character/entity references (maybe support all HTML entities by default?)
__construct($encoding=null) ¶Initializes the parser.
The constructor method instantiates and configures the underlying XML parser and its handlers.
$encodingThe character encoding to use for this parser. This parameter is optional (defaults to null) and can be safely omitted. See the PHP manual for xml_parser_create for more information about encodings.
feed($data) ¶Passes some data to the parser.
The parser will process it and invoke the appropriate handlers and callback methods.
$dataA string containing some data
feed_file($filename) ¶Passes the contents of a complete file to the parser.
The parser will process it and invoke the appropriate handlers and callback methods.
$filenameA string pointing to an XML file
close() ¶Tells the parser that there's no more input data.
If you override this method, make sure you call this one too by using parent::close();
unknown_starttag($name, $attr) ¶Called when an unhandled start tag is found.
Override this method if you want your parser to have a default start tag handler.
$nameThe name of the tag
$attrThe attributes for this tag
unknown_endtag($name) ¶Called when an unhandled end tag is found.
Override this method if you want your parser to have a default end tag handler.
$nameThe name of the tag
handle_data($data) [protected] ¶Handle character data.
This method can be overridden to do something sensible with character data from the input data.
$dataThe preprocessed data (with whitespace collapsed)
unknown_pi($name, $data) [protected] ¶Called when an unhandled processing instruction is found.
Override this method if you want your parser to have a default processing instruction handler.
$nameThe name of the processing instruction
$dataA string containing text data
handle_comment($data) [protected] ¶Called when comments are found in the input data.
$dataThe comment data (comment markers are already trimmed)
default_handler($data) [protected] ¶Default handler.
Override this method if you want to specify a default handler.
$dataA string containing text data
handle_error($errno, $errstr, $line, $col, $byte) [protected] ¶Default error handler.
Just throws a fatal error. Override this method if you want to do less intrusive things.
$errnoError number
$errstrError string
$lineLine where the error occurred
$colColumn where the error occurred
$byteByte offset where the error occurred
parent_tag($number_of_levels_up=1) [protected] ¶Return the current parent tag name.
An optional parameter can be specified to specify additional steps up the tree.
$number_of_levels_upControls how many levels up the tree (default is 1)
Tag name or null
current_tag() [protected] ¶Return the current tag name.
This can be useful when handling cdata.
Tag name or null
start_element_handler($parser, $name, $attr) [private] ¶Callback method for the XML parser.
$parserThe parser instance
$nameThe name of the tag
$attrThe attributes for this tag
end_element_handler($parser, $name) [private] ¶Callback method for the XML parser.
$parserThe parser instance
$nameThe name of the tag
_handle_data($parser, $data) [private] ¶Called when text data from the XML document is encountered.
This method stores the data in an internal buffer, which is flushed when a tag ends.
$parserThe parser instance
$dataA string containing text data
flush_data() [private] ¶Tells the parser to flush all buffered data.
This method is called when a tag ends to ensure data is processed in the correct order.
handle_pi($parser, $name, $data) [private] ¶Callback method for the XML parser.
$parserThe parser instance
$nameThe name of the processing instruction
$dataA string containing text data
_default_handler($parser, $data) [private] ¶Internal default handler preprocessor that looks for XML comments.
$parserThe parser instance
$dataA string containing text data
_handle_error() [private] ¶Internal error handler.
Collects error info and dispatches to the real error handler method.
$finalized [private] ¶Boolean indicating the parser state.
$cdata_buffer [private] ¶Character data buffer.