Search code examples
javaandroidprogressxmlpullparser

Is there any way to get the progress of an XMLPullParser function?


The parsing takes quite some time, and so I'd like to be able to display a progress bar. However I can't seem to find a way to get the progress to update. The issue comes from the fact that there doesn't seem to be a way (at least that I know of) to detect how many times pullparser.next() will be run. I've tried adding and counting newlines when the data is loaded initially, thought that doesn't give the correct result either.

int eventType = pullparser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
    if (eventType == XmlPullParser.START_TAG) {
        //Do stuff
    }
    eventType = pullparser.next();
    //Update progress by x%
}

Solution

  • I've devised a workaround for the moment, placing here if anyone stumbles to this thread. As long as you keep newlines in the original XML string: If you check that the eventype is text and that it is whitespace, and only increment the counter then, you can work out the progress.

    private int incrementPullParser(int eventType, XmlPullParser xpp) throws XmlPullParserException, IOException {
                if (eventType == XmlPullParser.TEXT && xpp.isWhitespace()){
                    //used for counting the progress
                    currentLines++;
                    publishProgress((double)(currentLines/maxLines)*100);
                    return xpp.next();
                }
                return eventType;
            }