Search code examples
androidxmlsaxsaxparser

Android: SAX parser progress monitoring


I have a SAX DefaultHandler which parses an InputStream. I don't know how many elements are in the XML so I can't count them on endElement or simmilar. I do know the byte length of the InputStream (read from the http header) but I can't find a method to get the current bytewise progress of the parsing process.

Is there a way to get the current progress (i.e. bits processed) of the parsing process?

This is how the DefaultHandler gets called:

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse(inputStream, myDefaultHandler);

Solution

  • You can do this by writing a FilterInputStream to wrap the existing inputStream. In the read() method of your filter, increment a counter, and provide a getter so that something else can track the current count.

    Since the parser may read ahead, this will be approximate, but it's probably the best you can do.