title: Android - XPath evaluate very slow
body: <p>I'm using <code>XPath</code> to query my <code>XML-file</code> which has at the moment about <code>100KB</code>.</p>
  
  <p>I'm iterating of an array and query for every value in the list. </p>
  
  <p><strong>Unfortunately a single query takes about 3-4 seconds</strong> under the debugger and slightly less with debugger disabled.</p>
  
  <p>Any ideas why this is so slow? I use a <code>Galaxy S2</code> for testing.</p>
  
  <p>Here's my code:</p>
  
  <pre><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&lt;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()&gt;0) {
          myElements.add((Element)nodes.item(0));
      }
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>When I leave out the <code>XPathConstants.NODESET</code> the evalution is done in no time but I don't get a <code>NodeList</code>. It returns an empty string instead...</p>