Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python handle endless XML

Tags:

python

xml

I am working on a application, and my job just is to develop a sample Python interface for the application. The application can provide XML-based document, I can get the document via HTTP Get method, but the problem is the XML-based document is endless which means there will be no end element. I know that the document should be handled by SAX, but how to deal with the endless problem? Any idea, sample code?

like image 209
Chris Avatar asked Jul 19 '10 19:07

Chris


1 Answers

This is what I use for parsing an endless xml stream which I get from a remote computer (in my case I connect over a socket and use socket.makefile('r') to create the file object)

19.12.2. IncrementalParser Objects

parser = xml.sax.make_parser(['xml.sax.IncrementalParser'])
handler = FooHandler()
parser.setContentHandler(handler)

data = sockfile.readline()
while ( len(data) != 0 ):
    parser.feed(data)
    data = sockfilefile.readline()
like image 56
getekha Avatar answered Sep 20 '22 05:09

getekha