Class for parsing XML documents using SAX

This class is a abstraction class for the PHP Expat XML functions.

You can define handler functions/objects for start, end, PI and data sections (1.) or your can define path which will trigger the defined event when encountered (2.)

Example:

1.) $parser->setEvents(array("startElement"=> "myFunction", "endElement"=> "myFunction", "characterData"=> "myFunction", "processingInstruction" => "myFunction");

The value can also be an array with the object reference and the method to call. i.e. "startElement"=>array(&$myObj, "myMethod") instead of "startelement"=>"myFunction"

2.) $parser->setEvents(array("/root/foo/bar"=>"myFunction"));

Valid array keys are: 'startElement', 'endElement', 'characterData', 'processingInstruction' and paths folowing the scheme '/root/element'. The path MUST begin from the root element and MUST start with '/'.

The value can also be an array with the object reference and the method to call. i.e. "/foo/bar"=>array(&$myObj, "myMethod") instead of "/foo/bar"=>"myFunction"

It has 3 public methods:

setEventHandlers - Set specific handlers for the xml parser parseFile - Used to parse a XML file parse - Used to parse a XML string

A small example:

include ("class.xmlparser.php");

// The XML String $xml = ' some text another text ';

function myHandler($name, $attribs, $content) { echo "HIT: [ $name ] [ $content ]<br/>"; }

$parser = new XmlParser; // Parser instance $parser->setEventHandlers(array("/foo/bar"=>"myHandler")); // Define our handler $parser->parse($xml); // Parse the XML string

Report bugs to: jan.lengowski@4fb.de

author Jan Lengowski <Jan.Lengowski@4fb.de>
copyright four for business AG <www.4fb.de>
version 1.0
package 4fb_XML

 Methods

Constructor function

XmlParser(string $sEncoding) : void

access private

Parameters

$sEncoding

string

Encoding used when parsing files (default: UTF-8, as in PHP5)

Add a path to the stack

_addPath(string $depth, $name) : void

access private

Parameters

$depth

string

Element node name

$name

Change all array keys to lowercase (PHP function change_key_case is available at PHP 4.2 +)

_changeKeyCase(array $options) : array

access private

Parameters

$options

array

Source array

Returns

arrayArray with lowercased keys

XML character data handler

_characterData(resource $parser, string $data) : void

access private

Parameters

$parser

resource

XML Parser resource

$data

string

XML node data

XML end element handler

_endElement(resource $parser, string $name) : void

access private

Parameters

$parser

resource

XML Parser resource

$name

string

XML Element node name

Returns the XML error message

_error() : string

access private

Returns

stringXML Error message

Returns the current active path

_getActivePath() 

access private

Return all defined paths from the options array and returns a new array containing only the paths to function bindings

_getDefinedPaths(array $options) : array

access private

Parameters

$options

array

Options array

Returns

arrayPaths array

Returns events handlers if set

_getEventHandler(string $event) : \sring

access private

Parameters

$event

string

Event type

Returns

\sringEvent handler name

Initialize the XML Parser object and sets all options

_init(string $sEncoding) : void

access private

Parameters

$sEncoding

string

Encoding used when parsing files (default: UTF-8, as in PHP5)

Set the processing instruction handler

_processingInstruction(string $parser, $target, $data) : void

access private

Parameters

$parser

string

Processing instruction handler

$target

$data

XML start element handler

_startElement(resource $parser, string $name, array $attribs) : void

access private

Parameters

$parser

resource

XML Parser resource

$name

string

XML Element node name

$attribs

array

XML Element node attributes

Parse a XML string

parse(string $data, $final) : bool

access public

Parameters

$data

string

XML data

$final

Returns

bool

Parse a XML file

parseFile(string $file) : bool

access public

Parameters

$file

string

File location

Returns

bool

Define events for the XML parser

setEventHandlers(array $options) : void

You can define handler functions/objects for start, end, PI and data sections (1.) or your can define path which will trigger the defined event when encountered (2.)

Example:

1.) $parser->setEvents(array("startElement" => "myFunction", "endElement" => "myFunction", "characterData" => "myFunction", "processingInstruction" => "myFunction");

The value can also be an array with the object reference and the method to call. i.e. "startElement"=>array(&$myObj, "myMethod") instead of "startelement"=>"myFunction"

2.) $parser->setEvents(array("/root/foo/bar"=>"myFunction"));

Valid array keys are: 'startElement', 'endElement', 'characterData', 'processingInstruction' and paths folowing the scheme '/root/element'. The path MUST begin from the root element and MUST start with '/'.

The value can also be an array with the object reference and the method to call. i.e. "/foo/bar"=>array(&$myObj, "myMethod") instead of "/foo/bar"=>"myFunction"

access public

Parameters

$options

array

Options array, valid keys are 'startElement', 'endElement', 'characterData', 'processingInstruction', or a path

 Properties

 

The active node

$activenode : string

access private
 

String storing the active path

$activepath : string

access private
 

XML Parser autofree

$autofree : bool

 

Element counter

$count : int

access private
 

Element depth

$depth : int

access private
 

Error message

$error : string

access private
 

The defined events

$events : array

access private
 

XML Parser object

$parser : object

access private
 

Data storage container for the path data

$pathdata : array

access private
 

Array for creating the path

$paths : array

access private
 

Path counter

$pcount : int

access private