tobeyt23 Posted October 16, 2009 Share Posted October 16, 2009 Has anyone used XML as a site controller and if so and references or ideas would be great. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 16, 2009 Share Posted October 16, 2009 Since XML is primarily designed to transport and store data and it does not do anything in itself, you would need to provide an example of what you are actually asking about. Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted October 16, 2009 Author Share Posted October 16, 2009 Instead of using a database to store data i was thinking use an xml file to hold articles then use php to read them. So i guess really I would use php as the controller and xml for the data Quote Link to comment Share on other sites More sharing options...
GoneNowBye Posted October 16, 2009 Share Posted October 16, 2009 You're in luck  <?php class XMLParser{   var $parser;   var $filePath;   var $document;   var $currTag;   var $tagStack; var $data; var $state;   function XMLParser($path,$state) {     $this->parser = xml_parser_create();   $this->filePath = $path;   $this->document = array();   $this->currTag =& $this->document;   $this->tagStack = array(); $this->state = $state;   }   function parse(){     xml_set_object($this->parser, $this);     xml_set_character_data_handler($this->parser, 'dataHandler');     xml_set_element_handler($this->parser, 'startHandler', 'endHandler');     if($this->state == 0) { if(!($fp = fopen($this->filePath, "r"))) { die("Cannot open XML data file: $this->filePath"); return false; } while($data = fread($fp, 40960)) { if(!xml_parse($this->parser, $data, feof($fp))) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } $this->data = $this->data.$data; fclose($fp); } } else { $data = $this->filePath; if(!xml_parse($this->parser, $data, false)) { die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($this->parser)), xml_get_current_line_number($this->parser))); } }         xml_parser_free($this->parser);       return true;   }   function startHandler($parser, $name, $attribs) {     if(!isset($this->currTag[$name]))       $this->currTag[$name] = array();         $newTag = array();     if(!empty($attribs))       $newTag['attr'] = $attribs;     array_push($this->currTag[$name], $newTag);         $t =& $this->currTag[$name];     $this->currTag =& $t[count($t)-1];     array_push($this->tagStack, $name);   }     function dataHandler($parser, $data){     $data = trim($data);         if(!empty($data))     {       if(isset($this->currTag['data']))         $this->currTag['data'] .= $data;       else         $this->currTag['data'] = $data;     }   }     function endHandler($parser, $name)   {     $this->currTag =& $this->document;     array_pop($this->tagStack);         for($i = 0; $i < count($this->tagStack); $i++)     {       $t =& $this->currTag[$this->tagStack[$i]];       $this->currTag =& $t[count($t)-1];     }   } } ?>  Enjoy, heres how to use. <?php $result1 = $xml_class_name->document['OPENINGTAG'][0]['ROW'][$x]['attr']['ATTRIBUTE']; //or data $result2 = $xml_class_name->document['OPENINGTAG'][0]['ROW'][$x]['data']; ?>  <openingtag> <row attribute="12">Some data</row> <row attribute="13">Some more</row> </openingtag>  Result1 = 12 assuming x is 0 Result2 = Some data assuming x is 0 Result1 = 13 assuming x is 1 Result2 = Some more assuming x is 1 Result1 & 2 = null assuming x is greater then 1   [EDIT] However MySQL is a hell of alot quicker. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted October 16, 2009 Share Posted October 16, 2009 However MySQL is a hell of alot quicker. I'll second that. Using some slow parsed/tokenized/interpreted php code to scan through and find information in xml data from a file is going to be about 100 times slower than using a database engine, which uses complied code to do the scanning and finding of information. So, if you expect the amount of data to continually grow and want your web site to perform well, use a database. Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted October 16, 2009 Author Share Posted October 16, 2009 I totally agree, but the person whom which this will be for doesn't want to use a db and wants to just update and edit a file to pull content. So I was thinking this would be a good workout. Quote Link to comment Share on other sites More sharing options...
GoneNowBye Posted October 16, 2009 Share Posted October 16, 2009 Do you follow my sollution? Quote Link to comment Share on other sites More sharing options...
tobeyt23 Posted October 16, 2009 Author Share Posted October 16, 2009 yes thanks Quote Link to comment Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 Although this deviates off the OP's question slightly, I thought this may be worth posting.  http://www.phpro.org/articles/Application-Configuration.html  It gives a comparison for storing application configuration information (not entire articles). While MySQL is definitely going to be faster for large sets of data, it is not necessarily faster for small amounts of information. Quote Link to comment Share on other sites More sharing options...
GoneNowBye Posted October 16, 2009 Share Posted October 16, 2009 If its not faster for small amounts of data, you dont have it indexed correctly  the only time that reading a file will be faster really is when you are reading it per line, and not sorting in anway;  I've used both and do use both, XML is better as a ... bridge, an API between two systems, MySQL is faster in every instance. Quote Link to comment Share on other sites More sharing options...
simshaun Posted October 16, 2009 Share Posted October 16, 2009 I'm not arguing with you, but care to provide a benchmark? Quote Link to comment Share on other sites More sharing options...
GoneNowBye Posted October 16, 2009 Share Posted October 16, 2009 sure i'll do one later. i'll post a graph. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.