javaandroidxmlxpathvtd-xml

Android - XPath evaluate very slow


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.

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

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

Here's my code:

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

Update

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...


Solution

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

    VTD-XML

    Here's a example from the devlopers.

    But this Tutorial is way better...