Class XML_Parser

Description

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.

  • todo: create XML_Parser_Namespace to parse documents with namespaces
  • version: Release: 1.4.9
  • copyright: 1997-2006 The PHP Group
  • 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
  • author: Tomas V.V.Cox <cox@idecnet.com>
  • author: Stig Bakken <ssb@fast.no>
  • author: Stephan Schmidt <schst@php-tools.net>

Located in /PEAR/XML/Parser.php (line 96)

PEAR
   |
   --XML_Parser
Direct descendents
Class Description
 class XML_Parser_Simple Simple XML parser class.
Variable Summary
 boolean $folding
 resource $fp
 array $handler
 string $mode
 resource $parser
 string $srcenc
 string $tgtenc
 object $_handlerObj
Method Summary
 XML_Parser __construct ([string $srcenc = null], [string $mode = 'event'], [mixed $tgtenc = null], string $tgenc)
 XML_Parser XML_Parser ([string $srcenc = null], [string $mode = 'event'], [mixed $tgtenc = null], string $tgenc)
 void endHandler (mixed $xp, mixed $elem)
 null free ()
 void funcEndHandler (mixed $xp, mixed $elem)
 void funcStartHandler (mixed $xp, mixed $elem, mixed $attribs)
 true|object PEAR parse ()
 Pear parseString (string $data, [boolean $eof = false])
 XML_Parser_Error raiseError ([string $msg = null], integer $ecode)
 boolean|object reset ()
 boolean setHandlerObj (object object &$obj)
 void setInput (mixed $fp)
 resource setInputFile (string $file)
 null setInputString (string $data)
 boolean|object setMode (string $mode)
 void startHandler (mixed $xp, mixed $elem, mixed &$attribs)
Variables
boolean $folding = true (line 123)

Whether to do case folding

If set to true, all tag and attribute names will be converted to UPPER CASE.

resource $fp (line 113)

File handle if parsing from a file

array $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'
)
(line 137)

Mapping from expat handler function to class method.


Redefined in descendants as:
string $mode (line 130)

Mode of operation, one of "event" or "func"

resource $parser (line 106)

XML parser handle

string $srcenc (line 151)

source encoding

string $tgtenc (line 158)

target encoding

object $_handlerObj (line 165)

handler object

Methods
Constructor __construct (line 199)

PHP5 constructor

XML_Parser __construct ([string $srcenc = null], [string $mode = 'event'], [mixed $tgtenc = null], string $tgenc)
  • string $srcenc: source charset encoding, use NULL (default) to use whatever the document specifies
  • string $mode: how this parser object should work, "event" for startelement/endelement-type events, "func" to have it call functions named after elements
  • string $tgenc: a valid target encoding
Constructor XML_Parser (line 183)

Creates an XML parser.

This is needed for PHP4 compatibility, it will call the constructor, when a new instance is created.

XML_Parser XML_Parser ([string $srcenc = null], [string $mode = 'event'], [mixed $tgtenc = null], string $tgenc)
  • string $srcenc: source charset encoding, use NULL (default) to use whatever the document specifies
  • string $mode: how this parser object should work, "event" for startelement/endelement-type events, "func" to have it call functions named after elements
  • string $tgenc: a valid target encoding
endHandler (line 622)
  • abstract:
void endHandler (mixed $xp, mixed $elem)
free (line 542)

XML_Parser::free()

Free the internal resources associated with the parser

null free ()
funcEndHandler (line 590)
void funcEndHandler (mixed $xp, mixed $elem)
funcStartHandler (line 574)
void funcStartHandler (mixed $xp, mixed $elem, mixed $attribs)
parse (line 457)

Central parsing function.

  • return: error returns true on success, or a PEAR_Error otherwise
  • access: public
true|object PEAR parse ()
parseString (line 517)

XML_Parser::parseString()

Parses a string.

  • return: Error|true true on success or a PEAR Error
  • see: _parseString()
  • throws: XML_Parser_Error
Pear parseString (string $data, [boolean $eof = false])
  • string $data: XML data
  • boolean $eof: If set and TRUE, data is the last piece of data sent in this parser
raiseError (line 564)

XML_Parser::raiseError()

Throws a XML_Parser_Error

XML_Parser_Error raiseError ([string $msg = null], integer $ecode)
  • string $msg: the error message
  • integer $ecode: the error message code

Redefinition of:
PEAR::raiseError()
This method is a wrapper that returns an instance of the configured error class with this object's default error handling applied. If the $mode and $options parameters are not specified, the object's defaults are used.
reset (line 353)

Reset the parser.

This allows you to use one parser instance to parse multiple XML documents.

  • return: true on success, PEAR_Error otherwise
  • access: public
boolean|object reset ()

Redefined in descendants as:
setHandlerObj (line 251)

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.

  • return: will always return true
  • since: v1.2.0beta3
  • access: public
boolean setHandlerObj (object object &$obj)
  • object object &$obj: to handle the events
setInput (line 425)

Sets the file handle to use with parse().

You should use setInputFile() or setInputString() if you pass a string

void setInput (mixed $fp)
  • mixed $fp: Can be either a resource returned from fopen(), a URL, a local filename or a string.
setInputFile (line 374)

Sets the input xml file to be parsed

resource setInputFile (string $file)
  • string $file: Filename (full path)
setInputString (line 404)

XML_Parser::setInputString()

Sets the xml input from a string

null setInputString (string $data)
  • string $data: a string containing the XML document
setMode (line 226)

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.

  • return: true on success, PEAR_Error otherwise
  • access: public
boolean|object setMode (string $mode)
  • string $mode: mode, either 'func' or 'event'
startHandler (line 610)
  • abstract:
void startHandler (mixed $xp, mixed $elem, mixed &$attribs)

Inherited Methods

Inherited From PEAR

 PEAR::PEAR()
 PEAR::delExpect()
 PEAR::expectError()
 PEAR::getStaticProperty()
 PEAR::isError()
 PEAR::loadExtension()
 PEAR::popErrorHandling()
 PEAR::popExpect()
 PEAR::pushErrorHandling()
 PEAR::raiseError()
 PEAR::registerShutdownFunc()
 PEAR::setErrorHandling()
 PEAR::staticPopErrorHandling()
 PEAR::staticPushErrorHandling()
 PEAR::throwError()
 PEAR::_PEAR()

Documentation generated on Mon, 04 Dec 2006 11:09:46 -0500 by phpDocumentor 1.3.0RC3