PHP Class XML_Parser

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 tags of your xml file.
Author: Stig Bakken ([email protected])
Author: Tomas V.V.Cox ([email protected])
Author: Stephan Schmidt ([email protected])
Inheritance: extends PEAR
Mostrar archivo Open project: sourcefabric/newscoop Class Usage Examples

Public Properties

Property Type Description
$_handlerObj object handler object
$_validEncodings array valid encodings
$folding boolean If set to true, all tag and attribute names will be converted to UPPER CASE.
$fp resource File handle if parsing from a file
$handler array Mapping from expat handler function to class method.
$mode string Mode of operation, one of "event" or "func"
$parser resource XML parser handle
$srcenc string source encoding
$tgtenc string target encoding

Public Methods

Method Description
XML_Parser ( string $srcenc = null, string $mode = 'event', string $tgtenc = null ) Creates an XML parser.
__construct ( string $srcenc = null, string $mode = 'event', string $tgtenc = null ) PHP5 constructor
_create ( ) : boolean | PEAR_Error create the XML parser resource
_initHandlers ( ) : mixed Init the element handlers
_parseString ( string $data, boolean $eof = false ) : boolean XML_Parser::_parseString()
endHandler ( mixed $xp, mixed $elem ) : null abstract method signature for End Handler
free ( ) : null XML_Parser::free()
funcEndHandler ( mixed $xp, mixed $elem ) : void derives and calls the End Handler function
funcStartHandler ( mixed $xp, mixed $elem, mixed $attribs ) : void derives and calls the Start Handler function
parse ( ) : boolean | PEAR_Error Central parsing function.
parseString ( string $data, boolean $eof = false ) : boolean | PEAR_Error XML_Parser::parseString()
raiseError ( $message = NULL, integer $ecode = NULL, $mode = NULL, $options = NULL, $userinfo = NULL, $error_class = NULL, $skipmsg = false ) : XML_Parser_Error XML_Parser::raiseError()
reset ( ) : boolean | object Reset the parser.
setHandlerObj ( &$obj ) : boolean Sets the object, that will handle the XML events
setInput ( mixed $fp ) : mixed Sets the file handle to use with parse().
setInputFile ( string $file ) : resource Sets the input xml file to be parsed
setInputString ( string $data ) : null XML_Parser::setInputString()
setMode ( string $mode ) : boolean | object Sets the mode of the parser.
startHandler ( mixed $xp, mixed $elem, &$attribs ) : null abstract method signature for Start Handler

Method Details

XML_Parser() public method

This is needed for PHP4 compatibility, it will call the constructor, when a new instance is created.
public XML_Parser ( string $srcenc = null, string $mode = 'event', string $tgtenc = null )
$srcenc string source charset encoding, use NULL (default) to use whatever the document specifies
$mode string how this parser object should work, "event" for startelement/endelement-type events, "func" to have it call functions named after elements
$tgtenc string a valid target encoding

__construct() public method

PHP5 constructor
public __construct ( string $srcenc = null, string $mode = 'event', string $tgtenc = null )
$srcenc string source charset encoding, use NULL (default) to use whatever the document specifies
$mode string how this parser object should work, "event" for startelement/endelement-type events, "func" to have it call functions named after elements
$tgtenc string a valid target encoding

_create() public method

Has been moved from the constructor to avoid problems with object references. Furthermore it allows us returning an error if something fails. NOTE: uses '@' error suppresion in this method
See also: xml_parser_create
public _create ( ) : boolean | PEAR_Error
return boolean | PEAR_Error true on success, PEAR_Error otherwise

_initHandlers() public method

Init the element handlers
public _initHandlers ( ) : mixed
return mixed

_parseString() public method

XML_Parser::_parseString()
See also: parseString()
public _parseString ( string $data, boolean $eof = false ) : boolean
$data string data
$eof boolean end-of-file flag
return boolean

endHandler() public method

abstract method signature for End Handler
public endHandler ( mixed $xp, mixed $elem ) : null
$xp mixed ??
$elem mixed ??
return null

free() public method

Free the internal resources associated with the parser
public free ( ) : null
return null

funcEndHandler() public method

derives and calls the End Handler function
public funcEndHandler ( mixed $xp, mixed $elem ) : void
$xp mixed ??
$elem mixed ??
return void

funcStartHandler() public method

derives and calls the Start Handler function
public funcStartHandler ( mixed $xp, mixed $elem, mixed $attribs ) : void
$xp mixed ??
$elem mixed ??
$attribs mixed ??
return void

parse() public method

Central parsing function.
public parse ( ) : boolean | PEAR_Error
return boolean | PEAR_Error returns true on success, or a PEAR_Error otherwise

parseString() public method

Parses a string.
See also: _parseString()
public parseString ( string $data, boolean $eof = false ) : boolean | PEAR_Error
$data string XML data
$eof boolean If set and TRUE, data is the last piece of data sent in this parser
return boolean | PEAR_Error true on success or a PEAR Error

raiseError() public method

Throws a XML_Parser_Error
public raiseError ( $message = NULL, integer $ecode = NULL, $mode = NULL, $options = NULL, $userinfo = NULL, $error_class = NULL, $skipmsg = false ) : XML_Parser_Error
$ecode integer the error message code
return XML_Parser_Error reference to the error object

reset() public method

This allows you to use one parser instance to parse multiple XML documents.
public reset ( ) : boolean | object
return boolean | object true on success, PEAR_Error otherwise

setHandlerObj() public method

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.
public setHandlerObj ( &$obj ) : boolean
return boolean will always return true

setInput() public method

You should use setInputFile() or setInputString() if you pass a string
See also: parse()
public setInput ( mixed $fp ) : mixed
$fp mixed Can be either a resource returned from fopen(), a URL, a local filename or a string.
return mixed

setInputFile() public method

Sets the input xml file to be parsed
public setInputFile ( string $file ) : resource
$file string Filename (full path)
return resource fopen handle of the given file

setInputString() public method

Sets the xml input from a string
public setInputString ( string $data ) : null
$data string a string containing the XML document
return null

setMode() public method

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.
public setMode ( string $mode ) : boolean | object
$mode string mode, either 'func' or 'event'
return boolean | object true on success, PEAR_Error otherwise

startHandler() public method

abstract method signature for Start Handler
public startHandler ( mixed $xp, mixed $elem, &$attribs ) : null
$xp mixed ??
$elem mixed ??
return null

Property Details

$_handlerObj public_oe property

handler object
public object $_handlerObj
return object

$_validEncodings public_oe property

valid encodings
public array $_validEncodings
return array

$folding public_oe property

If set to true, all tag and attribute names will be converted to UPPER CASE.
public bool $folding
return boolean

$fp public_oe property

File handle if parsing from a file
public resource $fp
return resource

$handler public_oe property

Mapping from expat handler function to class method.
public array $handler
return array

$mode public_oe property

Mode of operation, one of "event" or "func"
public string $mode
return string

$parser public_oe property

XML parser handle
See also: xml_parser_create()
public resource $parser
return resource

$srcenc public_oe property

source encoding
public string $srcenc
return string

$tgtenc public_oe property

target encoding
public string $tgtenc
return string