How To Read and Note EoF while using Python SAX Parser



I am working SAX Parser. Let's take an example of having Address book where I have many addresses in the XML file


1> First step is read XML File elements 2> The read data is put to a new file while reading between elements. 2.a For example, I read the data of address. So I read Address Line 1, Line 2 etc and then put it to a new file 3> The new file is opened and I will have count of addresses from each location.


The problem here I am facing is, every time a new address is encountered the file is refreshed and old address + new address is re-written to the file.


Similarly the count also happens the same way. At the end the entire data is collected, but rewriting the same data along with new data to the file is leading me to performance issues.


So what I plan to do is to set a flag till the point it already wrote and counted and add the new data with out considering the old one which is already there and should be there..


I am not sure how we can go about achieving it. Any help is greatly appreciated.


Logic:


Step 1> Use SAX Parser. write 'specific' contents to file A from .xml file. Step 2> Call a module that will read the new file A and do a count of each locality and put it to a dictionary and write it to file B


Technically what I am doing:


1> XML Parser Initiated. 2> startElement 3> characters 4> endElement 4.a> Checks if endElement is encountered. If yes, then 4.a.i> Here is where I execute Step 1 of logic I told. 4.a.ii> Here is where the Step 2 is executed


What goes behind the scene:


4> endElement 4.a> Checks if endElement is encountered. If yes, then 4.a.i> Here is where I execute Step 1 of logic I told. 4.a.ii> Here is where the Step 2 is executed


In the next cycle when the next element is read (say next address is read) it is refreshing the file (in windows explorer it shows 10 bytes -> 0bytes -> 14 bytes).


Meaning the old data is removed and from buffer old data + new data is put.


What I want:


I don't want to keep old data in buffer. I want new data and just add that new data to the file.


Tiada ulasan:

Catat Ulasan