title: Efficient SAX Handling
body: <p>I have a series of XMLs containing postcodes with their corresponding latitude and longitude, like so;</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;postcodes&gt;
      &lt;entry postcode='AB1 0AA' latitude='7.101478' longitude='2.242852' /&gt;
      &lt;entry postcode='AB1 0AB' latitude='7.201458' longitude='2.122952' /&gt;
  &lt;/postcodes&gt;
  </code></pre>
  
  <p>The XMLs are split into post codes beginning with a certain letter, so there is one XML for each letter in the alphabet. Between them, they have every post code in the UK, which means the largest of these XML files has 300,000 <code>entry</code> elements.</p>
  
  <p>I am looping through a list of Entity objects to put their post codes through SAX, to retrieve the <code>longitude</code> and <code>latitude</code> values against each post code. So, if I have 2000 entity objects, I am getting the SAX Handler to run 2000 times to retrieve those values. Code for the loop below;</p>
  
  <pre><code>em = emf.createEntityManager();
  
      for (Integer id : siteID){ 
              site = em.find(SiteTable.class, id);
              if(site != null &amp;&amp; site.getPostcode() != null &amp;&amp; !site.getPostcode().equals("")){
                  XMLPositionRetriever.runXMLQuery(site.getPostcode()); 
              }
              else{
                  System.out.println("The site and/or postcode against this Instruction does not exist.");
              }
       }
  em.close();
  </code></pre>
  
  <p><code>site.getPostcode()</code> becomes <code>postcodeToFind</code> in the Handler. Code for the only SAX Handler method being used below;</p>
  
  <pre><code>@Override 
  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
      if (postcodeToFind.equals(attributes.getValue("postcode"))){
          System.out.println("The postcode '"+postcodeToFind+"', has a latitude of "+attributes.getValue("latitude")+" and a longitude of "+attributes.getValue("longitude"));
          throw new SAXException();   
      }      
  }
  </code></pre>
  
  <p>Currently this is time consuming (it takes just under 4 minutes for 2000 searches), but I need the load times to be fast. Under 30 seconds preferably. So far, I have managed to cut the load times down well below half by;</p>
  
  <ul>
  <li>Cutting down the number of times the Handler has to run, to only the essential number of times (by reducing the number of entities needing to be checked).</li>
  <li>Making the startElement() method throw an exception once the data I need has been found so that it doesn't continue to search unnecessarily.</li>
  <li>Breaking the XML files into smaller files (one for each letter of the alphabet), so that the handler has fewer elements to check per file. </li>
  </ul>
  
  <p><strong>Q: Does anyone have any other suggestions for more efficient SAX handling?</strong></p>