0

I am trying to parse an web response with DOM parser like this:

public static Document parseDocument(InputStream sr) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setIgnoringComments(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setCoalescing(true);
    //dbf.setValidating(false);
    Document xdoc = dbf.newDocumentBuilder().parse(new InputSource(sr));
    xdoc.normalize();
    return xdoc;
}

The problem is that

Document xdoc = dbf.newDocumentBuilder().parse(new InputSource(sr));

takes 3 min to be executed. My xml file has 3800 lines. Is than normaé and how to improve this?

Thank you.

Milos Cuculovic
  • 19,631
  • 51
  • 159
  • 265

1 Answers1

1

This is too big for DOM parsing in android. Memory consumption and object allocation is porhobitive. SAX asin previous answer is workable solution, but Pull-Parser ( also contained in android APIs ) is better and more modern choice, as it is easier to program ( with SAX parser drives procesing and pushes XML events ti your handler, while XPP it is your application driving parser and pulling xml events out of stream )

Even better choice would be some lightweight data binding framework built on top of pull parser - jackson, xstream etc. They will deliver just objects - s you do not have to worry about xml parsing at all

Konstantin Pribluda
  • 12,329
  • 1
  • 30
  • 35