class XMLParser in Asset 6
Same name in this branch
- 6 asset_youtube/xmlparser.class.php \XMLParser
- 6 asset_youtube/inc/xmlparser.class.php \XMLParser
Same name and namespace in other branches
- 5 asset_youtube/xmlparser.class.php \XMLParser
XML Parser Class (php4)
Parses an XML document into an object structure much like the SimpleXML extension.
@author Adam A. Flynn <adamaflynn@criticaldevelopment.net> @copyright Copyright (c) 2006, Adam A. Flynn
@version 1.2.0
Hierarchy
- class \XMLParser
Expanded class hierarchy of XMLParser
File
- asset_youtube/
inc/ xmlparser.class.php, line 26
View source
class XMLParser {
/**
* The XML parser
*
* @var resource
*/
var $parser;
/**
* The XML document
*
* @var string
*/
var $xml;
/**
* Document tag
*
* @var object
*/
var $document;
/**
* Current object depth
*
* @var array
*/
var $stack;
/**
* Constructor. Loads XML document.
*
* @param string $xml The string of the XML document
* @return XMLParser
*/
function XMLParser($xml = '') {
//Load XML document
$this->xml = $xml;
// Set stack to an array
$this->stack = array();
}
/**
* Initiates and runs PHP's XML parser
*/
function Parse() {
//Create the parser resource
$this->parser = xml_parser_create();
//Set the handlers
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, 'StartElement', 'EndElement');
xml_set_character_data_handler($this->parser, 'CharacterData');
//Error handling
if (!xml_parse($this->parser, $this->xml)) {
$this
->HandleError(xml_get_error_code($this->parser), xml_get_current_line_number($this->parser), xml_get_current_column_number($this->parser));
}
//Free the parser
xml_parser_free($this->parser);
}
/**
* Handles an XML parsing error
*
* @param int $code XML Error Code
* @param int $line Line on which the error happened
* @param int $col Column on which the error happened
*/
function HandleError($code, $line, $col) {
trigger_error('XML Parsing Error at ' . $line . ':' . $col . '. Error ' . $code . ': ' . xml_error_string($code));
}
/**
* Gets the XML output of the PHP structure within $this->document
*
* @return string
*/
function GenerateXML() {
return $this->document
->GetXML();
}
/**
* Gets the reference to the current direct parent
*
* @return object
*/
function GetStackLocation() {
$return = '';
foreach ($this->stack as $stack) {
$return .= $stack . '->';
}
return rtrim($return, '->');
}
/**
* Handler function for the start of a tag
*
* @param resource $parser
* @param string $name
* @param array $attrs
*/
function StartElement($parser, $name, $attrs = array()) {
//Make the name of the tag lower case
$name = strtolower($name);
//Check to see if tag is root-level
if (count($this->stack) == 0) {
//If so, set the document as the current tag
$this->document = new XMLTag($name, $attrs);
//And start out the stack with the document tag
$this->stack = array(
'document',
);
}
else {
//Get the name which points to the current direct parent, relative to $this
$parent = $this
->GetStackLocation();
//Add the child
eval('$this->' . $parent . '->AddChild($name, $attrs, ' . count($this->stack) . ');');
//Update the stack
eval('$this->stack[] = $name.\'[\'.(count($this->' . $parent . '->' . $name . ') - 1).\']\';');
}
}
/**
* Handler function for the end of a tag
*
* @param resource $parser
* @param string $name
*/
function EndElement($parser, $name) {
//Update stack by removing the end value from it as the parent
array_pop($this->stack);
}
/**
* Handler function for the character data within a tag
*
* @param resource $parser
* @param string $data
*/
function CharacterData($parser, $data) {
//Get the reference to the current parent object
$tag = $this
->GetStackLocation();
//Assign data to it
eval('$this->' . $tag . '->tagData .= trim($data);');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
XMLParser:: |
property | Document tag | ||
XMLParser:: |
property | The XML parser | ||
XMLParser:: |
property | Current object depth | ||
XMLParser:: |
property | The XML document | ||
XMLParser:: |
function | Handler function for the character data within a tag | ||
XMLParser:: |
function | Handler function for the end of a tag | ||
XMLParser:: |
function | Gets the XML output of the PHP structure within $this->document | ||
XMLParser:: |
function | Gets the reference to the current direct parent | ||
XMLParser:: |
function | Handles an XML parsing error | ||
XMLParser:: |
function | Initiates and runs PHP's XML parser | ||
XMLParser:: |
function | Handler function for the start of a tag | ||
XMLParser:: |
function | Constructor. Loads XML document. |