Search code examples
androidxmlreadersaxparser

Android: why use XMLReader?


Is there any reason why I should use a XMLReader with the SAXParser? I'm seeing this kind of usage quite a lot:

        sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();
        LoginContentHandler uch = new LoginContentHandler();
        xr.setContentHandler(uch);
        xr.parse(new InputSource(in));

I always use the parser this way:

        sp = spf.newSAXParser();
        DefaultHandler dh = new LoginContentHandler();
        sp.parse(in, dh);

Any particular reason? Just wondering because my way is shorter, and I don't really see why I should use a XMLReader.


Solution

  • so to answer the question.

    People using XMLReader are assuming that there is enough memory to store the file as a tree object and be able to traverse it at Log(n) time for searching.

    On the other hand using SAX straight would mean you are using a event based plugin which would be in n time, but should leave a much less memory foot print.

    It's a choice between space and speed.

    note the reading about this from SAX's own page: