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 ]
"; } $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
version 1.0
package CONTENIDO Backend Classes
deprecated 2012-08-24 Use cXmlReader instead

 Methods

Constructor function

XmlParser(string $sEncoding) : void
access private
deprecated 2012-08-24 Use cXmlReader instead

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
deprecated 2012-08-24 Use cXmlReader instead

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
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$options

array

Source array

Returns

arrayArray with lowercased keys

XML character data handler

_characterData(resource $parser, string $data) : void
access private
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$parser

resource

XML Parser resource

$data

string

XML node data

XML end element handler

_endElement(resource $parser, string $name) : void
access private
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$parser

resource

XML Parser resource

$name

string

XML Element node name

Returns the XML error message

_error() : string
access private
deprecated 2012-08-24 Use cXmlReader instead

Returns

stringXML Error message

Returns the current active path

_getActivePath() 
access private
deprecated 2012-08-24 Use cXmlReader instead

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
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$options

array

Options array

Returns

arrayPaths array

Returns events handlers if set

_getEventHandler(string $event) : \sring
access private
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$event

string

Event type

Returns

\sringEvent handler name

Initialize the XML Parser object and sets all options

_init(string $sEncoding) : void
access private
deprecated 2012-08-24 Use cXmlReader instead

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
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$parser

string

Processing instruction handler

$target

$data

XML start element handler

_startElement(resource $parser, string $name, array $attribs) : void
access private
deprecated 2012-08-24 Use cXmlReader instead

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
deprecated 2012-08-24 Use cXmlReader instead

Parameters

$data

string

XML data

$final

Returns

bool

Parse a XML file

parseFile(string $file) : bool
access public
deprecated 2012-08-24 Use cXmlReader instead

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"

deprecated 2012-08-24 Use cXmlReader instead

Parameters

$options

array

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

 Properties

 

$activenode : string
access private
 

$activepath : string
access private
 

$autofree : bool
 

$count : int
access private
 

$depth : int
access private
 

$error : string
access private
 

$events : array
access private
 

$parser : object
access private
 

$pathdata : array
access private
 

$paths : array
access private
 

$pcount : int
access private