contenido\classes\class.xmlparser.php
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
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 = '
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

array $pathdata = array()array()Details- Type
- array
- Access
- private
Methods

XmlParser(string $sEncoding = false) : voidConstructor function
| Name | Type | Description |
|---|---|---|
| $sEncoding | string | Encoding used when parsing files (default: UTF-8, as in PHP5) |
- Access
- private

_addPath(string $depth, $name) : voidAdd a path to the stack
| Name | Type | Description |
|---|---|---|
| $depth | string | Element node name |
| $name |
- Access
- private

_changeKeyCase(array $options = array()) : arrayChange all array keys to lowercase (PHP function change_key_case is available at PHP 4.2 +)
| Name | Type | Description |
|---|---|---|
| $options | array | Source array |
| Type | Description |
|---|---|
| array | Array with lowercased keys |
- Access
- private

_characterData(resource $parser, string $data) : voidXML character data handler
| Name | Type | Description |
|---|---|---|
| $parser | resource | XML Parser resource |
| $data | string | XML node data |
- Access
- private

_endElement(resource $parser, string $name) : voidXML end element handler
| Name | Type | Description |
|---|---|---|
| $parser | resource | XML Parser resource |
| $name | string | XML Element node name |
- Access
- private

_error() : stringReturns the XML error message
| Type | Description |
|---|---|
| string | XML Error message |
- Access
- private

_getDefinedPaths(array $options) : arrayReturn all defined paths from the options array and returns a new array containing only the paths to function bindings
| Name | Type | Description |
|---|---|---|
| $options | array | Options array |
| Type | Description |
|---|---|
| array | Paths array |
- Access
- private

_getEventHandler(string $event) : \sringReturns events handlers if set
| Name | Type | Description |
|---|---|---|
| $event | string | Event type |
| Type | Description |
|---|---|
| \sring | Event handler name |
- Access
- private

_init(string $sEncoding = false) : voidInitialize the XML Parser object and sets all options
| Name | Type | Description |
|---|---|---|
| $sEncoding | string | Encoding used when parsing files (default: UTF-8, as in PHP5) |
- Access
- private

_processingInstruction(string $parser, $target, $data) : voidSet the processing instruction handler
| Name | Type | Description |
|---|---|---|
| $parser | string | Processing instruction handler |
| $target | ||
| $data |
- Access
- private

_startElement(resource $parser, string $name, array $attribs) : voidXML start element handler
| Name | Type | Description |
|---|---|---|
| $parser | resource | XML Parser resource |
| $name | string | XML Element node name |
| $attribs | array | XML Element node attributes |
- Access
- private

parse(string $data, $final = false) : boolParse a XML string
| Name | Type | Description |
|---|---|---|
| $data | string | XML data |
| $final |
| Type | Description |
|---|---|
| bool |
- Access
- public

parseFile(string $file) : boolParse a XML file
| Name | Type | Description |
|---|---|---|
| $file | string | File location |
| Type | Description |
|---|---|
| bool |
- Access
- public

setEventHandlers(array $options = array(NULL)) : voidDefine 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"
| Name | Type | Description |
|---|---|---|
| $options | array | Options array, valid keys are 'startElement', 'endElement', 'characterData', 'processingInstruction', or a path |
- Access
- public