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) : void
Constructor function
Name | Type | Description |
---|---|---|
$sEncoding | string | Encoding used when parsing files (default: UTF-8, as in PHP5) |
- Access
- private


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


_changeKeyCase(array $options = array()) : array
Change 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) : void
XML character data handler
Name | Type | Description |
---|---|---|
$parser | resource | XML Parser resource |
$data | string | XML node data |
- Access
- private


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


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


_getDefinedPaths(array $options) : array
Return 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) : \sring
Returns events handlers if set
Name | Type | Description |
---|---|---|
$event | string | Event type |
Type | Description |
---|---|
\sring | Event handler name |
- Access
- private


_init(string $sEncoding = false) : void
Initialize 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) : void
Set the processing instruction handler
Name | Type | Description |
---|---|---|
$parser | string | Processing instruction handler |
$target | ||
$data |
- Access
- private


_startElement(resource $parser, string $name, array $attribs) : void
XML 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) : bool
Parse a XML string
Name | Type | Description |
---|---|---|
$data | string | XML data |
$final |
Type | Description |
---|---|
bool |
- Access
- public


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


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