2012-02-22 61 views
0

我试图解析与DOM解析器像这样的网络响应:Android的DOM解析是tooo慢

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; 
} 

的问题是,

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

需要3分钟被执行。我的xml文件有3800行。 是否比normaé以及如何改进?

谢谢。

回答

1

这对于android中的DOM解析来说太大了。内存消耗和对象分配是多余的。 SAX asin以前的答案是可行的解决方案,但Pull-Parser(也包含在android API中)是更好和更现代的选择,因为它更容易编程 (使用SAX解析器驱动器处理并将XML事件推送到您的处理程序,而XPP它是你的应用程序驱动分析器和拉动xml事件的流)

更好的选择将是一些轻量级的数据绑定框架构建在拉解析器 - 杰克逊,xstream之上等。他们将只提供对象 - 你没有担心xml解析在所有

+0

谢谢你这个答案。我在哪里可以找到一个合适的Pull-Parser例子? – 2012-02-22 14:18:36

+0

只是谷歌的产品。有样品 – 2012-02-22 18:39:07