< SAX Parser Concepts   (Previous) Table of Contents (Next)   Parser: Start Tag Handler >

Parser: Define and Execute

/**
 * Parses the data.
 *
 * <p>Sets up the requisite sax XML parsing functions then passes
 * the XML data accumulated in <var>$ContentsRaw</var> to the parser.
 * Once done, reset several class variables to their default values.</p>
 *
 * @return  boolean  true if no problems, false if problems
 */
function runParser() {
    /*
     * Replace all non-visible characters (except SP, TAB, LF and CR)
     * with LF to keep the sax parser from choking.
     */
    $this->Contents = trim( preg_replace('/[^\x20-\x7E\x09\x0A\x0D]/', "\n",
            $this->ContentsRaw) );

    $this->Contents = preg_replace('/&amp;|&/i', '&amp;', $this->Contents);

    $this->Parser = xml_parser_create('UTF-8');
    xml_set_object($this->Parser, $this);
    xml_set_element_handler($this->Parser, 'saxStartHandler', 'saxEndHandler');
    xml_set_character_data_handler($this->Parser, 'saxCharacterHandler');

    if ( !xml_parse($this->Parser, $this->Contents, TRUE) ) {
        $this->Probs[] = "File rejected by parser:\n   "
                . xml_error_string( xml_get_error_code($this->Parser) );
    }

    xml_parser_free($this->Parser);

    $ProbCount = count($this->Probs);
    if ($ProbCount != 0) {
        //  Error handling omitted for clarity.
    }

    $this->IgnoreTheRest  = 'N';
    $this->Contents       = '';
    $this->ContentsRaw    = '';
    $this->Data           = array();
    $this->ParentElements = array();
    $this->Probs          = array();

    return ($ProbCount == 0);
}