[ create a new paste ] login | about

Link: http://codepad.org/4o1FnMnY    [ raw code | fork ]

ninwa - PHP, pasted on May 8:
<?php
	/*  Parse QFX files, but really only parse the   
	 * 	part of the XML file we really care about
	 *  <STMTTRN>
     *   <TRNTYPE>DEBIT</TRNTYPE>
     *   <DTPOSTED>20120430</DTPOSTED>
     *   <TRNAMT>-12.99</TRNAMT>
     *   <FITID>201204301010</FITID>
     *   <NAME>POS PURCHASE</NAME>
     *   <MEMO>PAYPAL *YALL INC San Jose CA</MEMO>
     *  </STMTTRN>
     */
     
    class TransEntry{
		public $trntype;
		public $dtposted;
		public $trnamt;
		public $fitid;
		public $name;
		public $memo;
		
		function __construct(){
			$this->trntype = null;
			$this->dtposted = null;
			$this->trnamt = null;
			$this->fitid = null;
			$this->name = null;
			$this->memo = null;
		}
	};
     
	class QFXParser {
		private $file;
		private $parser;
		private $in;
		private $entries;
		
		private function startTag($parser, $name, $attrib){
			$this->in = $name;			
		}
		
		private function endTag($parser, $name){
			if($name == "STMTTRN")
				$this->entries[] = new TransEntry();
		}
		
		private function dataHandler($parser, $data){
			$in = strtolower($this->in);
			if(count($this->entries) > 0){
				$sz = count($this->entries);
				if(in_array($in, $this->data) && strlen($data) > 0){
					$this-> in = null; 
					$this->entries[$sz-1]->$in = $data;
				}
			}		
		}
		
		function parse(){
			if($this->file == null)
				throw new Exception('Could not parse, invalid file handler.');
			else{		
				
				/* Let's move the pointer past the non-xml formatted header *
				 * In the future, maybe consider parsing it... but let's
				 * be honest... we probably (read definitely) never will. */ 
				for($i = 0; $i < 10; $i++){
					fgets($this->file);
				}
				
				while($data = fread($this->file, 4096)){
					xml_parse($this->parser, $data, feof($this->file));
				}

			}
		}
		
		function getEntries(){
			return $this->entries;
		}
		
		function __construct($file){
			$this->file = fopen($file, 'r');
			$this->parser = xml_parser_create(); 
			$this->entries = Array();
			$this->entries[] = new TransEntry();
			$this->in = null;
			$this->data = Array('trntype', 'trnamt', 'fitid', 
			                    'name', 'memo', 'dtposted');
			
			xml_set_object($this->parser, &$this);			                        
			xml_set_element_handler( $this->parser,
			                         "startTag",
			                         "endTag" );
			xml_set_character_data_handler( $this->parser,
			                                "dataHandler" );
		}
		
		function __destruct(){
			xml_parser_free($this->parser);
			if($this->file)
				fclose($this->file);
		}
	};
	


Create a new paste based on this one


Comments: