You are here

class XMLParser in Asset 5

Same name and namespace in other branches
  1. 6 asset_youtube/xmlparser.class.php \XMLParser
  2. 6 asset_youtube/inc/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

Expanded class hierarchy of XMLParser

File

asset_youtube/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

Namesort descending Modifiers Type Description Overrides
XMLParser::$document property Document tag
XMLParser::$parser property The XML parser
XMLParser::$stack property Current object depth
XMLParser::$xml property The XML document
XMLParser::CharacterData function Handler function for the character data within a tag
XMLParser::EndElement function Handler function for the end of a tag
XMLParser::GenerateXML function Gets the XML output of the PHP structure within $this->document
XMLParser::GetStackLocation function Gets the reference to the current direct parent
XMLParser::HandleError function Handles an XML parsing error
XMLParser::Parse function Initiates and runs PHP's XML parser
XMLParser::StartElement function Handler function for the start of a tag
XMLParser::XMLParser function Constructor. Loads XML document.