All pastes #840252 Raw Edit

Anonymous

public text v1 · immutable
#840252 ·published 2007-12-31 17:14 UTC
rendered paste body
<?
/**
*	okay your task would be to make an XML parser:
*	It must be using the xml extension only for php: http://www.php.net/ref.xml
*	It must convert all input into UTF-8 or Unicode when reading
*	It must be able to load any xml content as long its valid xml
*	It must contain an error handler, eg. use the libxml extension for php: http://www.php.net/ref.libxml
*	It must be in OOP using PHP5
*	 When getting the parsed data it must be returned in an object using method overloading like simplexml does
**/
class mccms_xmlParser {
	
	private $cur;
	
	public $xml_parser;
	
	private $xml_f;
	
	public $data;
	
	private $cur_line = 0;
	
	public function FoundStartTag($parser,$name,$attribs){
		$this->cur_line ++;
		if($this->cur_line = '1'){
		echo "<br />";
		$indent = '&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		echo "&lt;<font color=\"#0000cc\">$name</font>&gt;";
    if (count($attribs)) {
        foreach ($attribs as $k => $v) {
            echo " <font color=\"#009900\">$k</font>=\"<font 
                   color=\"#990000\">$v</font>\"";
        }
    }
	}
	
	
	public function FoundTagData($parser,$data){
		echo " <b>$data</b> ";
	}
	public function FoundEndTag($parser,$name){
		echo "&lt;/<font color=\"#0000cc\">$name</font>&gt;";
		$this->cur_line--;
		$num ++;
	}
	
	
	public function parse($file,$encode = 'UTF-8'){
		if(!$this->xml_f = @fopen($file,"r")){
			die('Error opening '.$file);	
		}
		
		$this->xml_parser = xml_parser_create($encode);
		//Set some infomationz
		xml_set_object($this->xml_parser,$this);
		xml_set_element_handler($this->xml_parser,'FoundStartTag','FoundEndTag');
		xml_set_character_data_handler($this->xml_parser,'FoundTagData');
		$size = filesize($file);
while ($data = fread($this->xml_f, 4096)) {
   	 xml_parse($this->xml_parser, $data, feof($this->xml_f));
}

		//clears out the current XML parser
		xml_parser_free($this->xml_parser);
	}
	
	
	
	

}
$xml = new mccms_xmlParser;
$xml->parse('xml.xml');



?>