/**
* 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 "
";
$indent = ' ';
}
echo "<$name>";
if (count($attribs)) {
foreach ($attribs as $k => $v) {
echo " $k=\"$v\"";
}
}
}
public function FoundTagData($parser,$data){
echo " $data ";
}
public function FoundEndTag($parser,$name){
echo "</$name>";
$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');
?>