<?php
	defined( "_VALID_PAGE_MONITOR" ) or die (_NODIRECTACCESS);
		
	class XMLParser{
		var $parser;
			// PHP XML parser.
			
		var $data;
		var $stack;
		var $current;
		var $first;
		
		function XMLParser($path){
			$this->parser = xml_parser_create();
			$this->stack = array();
			$this->data = array();
			
			xml_set_object($this->parser, $this);
			xml_set_element_handler($this->parser, 'startXML', 'endXML');
			xml_set_character_data_handler($this->parser, 'charXML');
			xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, false);

			$xmlFileAsString = file_get_contents($path);
			$lines = explode("\n",$xmlFileAsString);
			foreach( $lines as $val ){
				if( trim($val) == '' )
					continue;
				else
					$data = $val . "\n";
					if( !xml_parse($this->parser, $data) ){
					$this->error(sprintf('XMLError at line %d, column %d',
						xml_get_current_line_number($this->parser),
						xml_get_current_column_number($this->parser)));
				}
			}
		}
		
		function error($str){
			echo $str . "<br/>";
		}
		
		function charXML($parser, $data){
			$this->current["data"] = $data;
		}
		
		function startXML($parser, $name, $attr){
		/*
			$uid = uniqid();
			
			
			if( !count($this->stack) ){
				$this->data[$uid] = $array;
				echo 'once';
				$this->first = $uid;
			}else{
				$e = $this->searchForElementWithUID($this->stack[count($this->stack)-1], NULL);
				$e["children"] = $array;
			//	$this->
		//		array_push($this->stack, $this->data[0]);
			}
			
				array_push($this->stack, $uid);

			//print_r($this->data);
			echo "<br/><br/>";
			*/
			$array = array("name" => $name, "attributes" => $attr, "children" => array() );
			$this->current = $array;
		}
		
		function endXML($parser, $data){
			//echo "end " . $data . "<br/>";
			//array_pop( $this->stack );
			$this->data[] = $this->current;
		}
		
		/*
		function searchForElementWithUID($uid, $startingElement){
			if( $startingElement == NULL )
				$startingElement = $this->data[$this->first];
				
			if( $startingElement["uid"] == $uid )
				return $startingElement;
				
			foreach( $startingElement as $key => $value ){
				$val = $this->searchForElementWithUID($uid, $value);
				if( $val != NULL )
					return $val;
			}
			return NULL;
		}
		*/
		
		function getRoot(){
			return $this->data;
		}
	}
?>