我正在尝试使用DOM解析器解析Web响应,如下所示:
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é还要怎么改进呢?
谢谢。
答案 0 :(得分:1)
这对于android中的DOM解析来说太大了。内存消耗和对象分配是高级的。 SAX asin以前的答案是可行的解决方案,但Pull-Parser(也包含在android API中)是更好更现代的选择,因为它更容易编程 (使用SAX解析器驱动处理并向您的处理程序推送XML事件,而XPP则是您的应用程序驱动解析器并将xml事件从流中拉出来)
更好的选择是建立在pull解析器之上的一些轻量级数据绑定框架 - jackson,xstream等。它们只提供对象 - 你根本不必担心xml解析