Location: PHPKode > projects > Community Learning Network > cln/lib/CLN/SimpleXMLParser.php
<?php
/*
 * SimpleXMLParser Class
 *
 * Copyright (c) 2003-4 St. Christopher House
 *
 * Developed by The Working Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * @version $Id: SimpleXMLParser.php,v 1.4 2004/11/19 04:41:21 cbooth7575 Exp $
 *
 */

require_once 'PEAR.php';

class SimpleXMLParser extends PEAR {

    var $parser;
    var	$tag_open = 'tag_open';
    var	$tag_close = 'tag_close';
    var	$cdata = 'cdata';
    var	$pihandler = 'pihandler';
	var $content = '';

	/*
	 *
	 * Constructor:  SimpleXMLParser()
	 *
	 * 		parses xml
	 *
	 * @access public
	 * @return string   $content
	 *
	 */
    function SimpleXMLParser()
    {
        $this->_PEAR();
        $this->parser = xml_parser_create();

        xml_set_object($this->parser, $this);
        xml_set_element_handler($this->parser, $this->tag_open, $this->tag_close);
        xml_set_character_data_handler($this->parser, $this->cdata);
        xml_set_processing_instruction_handler($this->parser, $this->pihandler);
    }

    // }}}
    // {{{ destructor
    /**
    * Destructor
    */
    function _SimpleXMLParser()
    {
    	$this->_PEAR();
    }



    function parse($data)
    {
        if(!xml_parse($this->parser, $data)) {
			$errorString =  xml_error_string(xml_get_error_code($this->parser))
							.' on line '.xml_get_current_line_number($this->parser);
            $this->raiseError("Raising error in the SimpleXMLParser: $errorString with data:<br/>".$this->prettyPrint($data), E_USER_ERROR);
		}

		return $this->content;
    }

    function tag_open($parser, $tag, $attributes)
    {
		$attr = '';
		while(list($key,$value) = each($attributes)) {
			$attr .= ' '.$key.'="'.$value.'"';
		}

		$this->content .= '<'.$tag.$attr.">\n";
    }

    function cdata($parser, $cdata)
    {
		$this->content .= $cdata;
    }

    function pihandler($parser, $target, $data)
    {
    	switch (strtolower($target)) {
        	case 'php':
				$this->content .= eval($data);
				break;
    	}
    }

    function tag_close($parser, $tag)
    {
		$this->content .= '</'.$tag.">\n";
    }

    function prettyPrint($data) {
    	$data = ereg_replace('<','&lt;',$data);
    	$data = ereg_replace('>','&gt;',$data);

    	return $data;
    }

} // end of class xml

?>
Return current item: Community Learning Network