本文介绍了Android - XPath评估速度很慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XPath 来查询我的 XML文件目前大约 100KB

I'm using XPath to query my XML-file which has at the moment about 100KB.

我正在迭代一个数组并查询列表中的每个值。

I'm iterating of an array and query for every value in the list.

不幸的是,在调试器下单个查询需要大约3-4秒,而在调试器禁用的情况下则略少一些。

Unfortunately a single query takes about 3-4 seconds under the debugger and slightly less with debugger disabled.

任何想法为什么这么慢?我使用 Galaxy S2 进行测试。

Any ideas why this is so slow? I use a Galaxy S2 for testing.

这是我的代码:

XPath xpath = XPathFactory.newInstance().newXPath();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document document = builder.parse(new File(file_on_internal_sd_url)));

int size = mPrefs.getInt("no_ids", 0);
for(int i=0;i<size;i++) {
    String id= mPrefs.getString("id_" + i, null);
    String expression = "/tag1/tag2[@id = '" + id+ "']";
    NodeList nodes = (NodeList) xpath.evaluate(expression, document, XPathConstants.NODESET);
    if(nodes.getLength()>0) {
        myElements.add((Element)nodes.item(0));
    }
}

更新

当我省略 XPathConstants.NODESET 时,评估很快就完成但我没有得到节点列表。它返回一个空字符串而不是...

When I leave out the XPathConstants.NODESET the evalution is done in no time but I don't get a NodeList. It returns an empty string instead...

推荐答案

感谢@nvrmnd我尝试了一下,找到了更好的解析器:

Thanks to @nvrmnd I tried a bit around and found a way better parser:

以下是来自devlopers的 。

Here's a example from the devlopers.

但这更好......

But this Tutorial is way better...

希望这有助于任何人像我一样感到沮丧......

Hope this helps anyone being as frustrated as I was...

这篇关于Android - XPath评估速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:21