contenido\classes\class.xmlparser.php

Show: PublicProtectedPrivateinherited
Table of Contents
Project: Contenido Content Management System

Description: Contenido XML Parser

Requirements:

Author
Jan Lengowski  
Con_php_req
5.0  
Copyright
four for business AG  
License
http://www.contenido.org/license/LIZENZ.txt  
Link
http://www.4fb.de  
Link
http://www.contenido.org  
Package
Contenido Backend classes  
Since
file available since contenido release <= 4.6 {@internal created unknown modified 2008-06-30, Dominik Ziegler, add security fix $Id: class.xmlparser.php 387 2008-06-30 10:01:05Z timo.trautmann $: }}  
Version
1.0.9  

\XmlParser

Package: 4fb\XML
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  
Copyright
four for business AG  
Version
1.0  

Properties

>VPropertypublicstring $activenode = ''
The active node
Default value''Details
Type
string
Access
private  
>VPropertypublicstring $activepath = ''
String storing the active path
Default value''Details
Type
string
Access
private  
>VPropertypublicbool $autofree = true
XML Parser autofree
Default valuetrueDetails
Type
bool
>VPropertypublicint $count = 0
Element counter
Default value0Details
Type
int
Access
private  
>VPropertypublicint $depth = -1
Element depth
Default value-1Details
Type
int
Access
private  
>VPropertypublicstring $error = ''
Error message
Default value''Details
Type
string
Access
private  
>VPropertypublicarray $events = array()
The defined events
Default valuearray()Details
Type
array
Access
private  
>VPropertypublicobject $parser
XML Parser object
Details
Type
object
Access
private  
>VPropertypublicarray $pathdata = array()
Data storage container for the path data
Default valuearray()Details
Type
array
Access
private  
>VPropertypublicarray $paths = array()
Array for creating the path
Default valuearray()Details
Type
array
Access
private  
>VPropertypublicint $pcount = 0
Path counter
Default value0Details
Type
int
Access
private  

Methods

methodpublicXmlParser(string $sEncoding = false) : void

Constructor function

Parameters
NameTypeDescription
$sEncodingstring

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

Details
Access
private  
methodpublic_addPath(string $depth,  $name) : void

Add a path to the stack

Parameters
NameTypeDescription
$depthstring

Element node name

$name
Details
Access
private  
methodpublic_changeKeyCase(array $options = array()) : array

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

Parameters
NameTypeDescription
$optionsarray

Source array

Returns
TypeDescription
arrayArray with lowercased keys
Details
Access
private  
methodpublic_characterData(resource $parser, string $data) : void

XML character data handler

Parameters
NameTypeDescription
$parserresource

XML Parser resource

$datastring

XML node data

Details
Access
private  
methodpublic_endElement(resource $parser, string $name) : void

XML end element handler

Parameters
NameTypeDescription
$parserresource

XML Parser resource

$namestring

XML Element node name

Details
Access
private  
methodpublic_error() : string

Returns the XML error message

Returns
TypeDescription
stringXML Error message
Details
Access
private  
methodpublic_getActivePath() : void

Returns the current active path

Details
Access
private  
methodpublic_getDefinedPaths(array $options) : array

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

Parameters
NameTypeDescription
$optionsarray

Options array

Returns
TypeDescription
arrayPaths array
Details
Access
private  
methodpublic_getEventHandler(string $event) : \sring

Returns events handlers if set

Parameters
NameTypeDescription
$eventstring

Event type

Returns
TypeDescription
\sringEvent handler name
Details
Access
private  
methodpublic_init(string $sEncoding = false) : void

Initialize the XML Parser object and sets all options

Parameters
NameTypeDescription
$sEncodingstring

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

Details
Access
private  
methodpublic_processingInstruction(string $parser,  $target,  $data) : void

Set the processing instruction handler

Parameters
NameTypeDescription
$parserstring

Processing instruction handler

$target
$data
Details
Access
private  
methodpublic_startElement(resource $parser, string $name, array $attribs) : void

XML start element handler

Parameters
NameTypeDescription
$parserresource

XML Parser resource

$namestring

XML Element node name

$attribsarray

XML Element node attributes

Details
Access
private  
methodpublicparse(string $data,  $final = false) : bool

Parse a XML string

Parameters
NameTypeDescription
$datastring

XML data

$final
Returns
TypeDescription
bool
Details
Access
public  
methodpublicparseFile(string $file) : bool

Parse a XML file

Parameters
NameTypeDescription
$filestring

File location

Returns
TypeDescription
bool
Details
Access
public  
methodpublicsetEventHandlers(array $options = array(NULL)) : void

Define events for the XML parser

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"

Parameters
NameTypeDescription
$optionsarray

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

Details
Access
public  
Documentation was generated by phpDocumentor 2.0.0a12.