title: How to improve StAX xml parser speed in java?
body: <p>I have a XML parser using <code>StAX</code> and I am using it to parse a huge file. However, I want to bring  the time down as low as possible. I am reading the values putting it into an array and sending it off to another function to evaluate. I am calling the <code>displayName</code> tag and it should go to the next xml as soon as it grabs the name instead of reading the whole xml file. I am looking for the fastest approach.</p>
  
  <p><strong>Java:</strong></p>
  
  <pre><code>
  import java.io.File;
  
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.util.Iterator;
  import javax.xml.namespace.QName;
  import javax.xml.stream.XMLEventReader;
  import javax.xml.stream.XMLInputFactory;
  import javax.xml.stream.XMLStreamException;
  import javax.xml.stream.events.*;
  
  public class Driver {
  
      private static boolean bname;
  
      public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
  
          File file = new File("C:\\Users\\Robert\\Desktop\\root\\SDKCode\\src\\main\\java\\com\\example\\xmlClass\\data.xml");
  
  
          parser(file);
      }
  
      public static void parser(File file) throws FileNotFoundException, XMLStreamException {
  
          bname = false;
  
  
          XMLInputFactory factory = XMLInputFactory.newInstance();
  
  
          XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(file));
  
  
          while (eventReader.hasNext()) {
  
              XMLEvent event = eventReader.nextEvent();
  
              // This will trigger when the tag is of type &lt;...&gt;
              if (event.isStartElement()) {
                  StartElement element = (StartElement) event;
  
  
                  Iterator&lt;Attribute&gt; iterator = element.getAttributes();
                  while (iterator.hasNext()) {
                      Attribute attribute = iterator.next();
                      QName name = attribute.getName();
                      String value = attribute.getValue();
                      System.out.println(name + " = " + value);
                  }
  
  
                  if (element.getName().toString().equalsIgnoreCase("displayName")) {
                      bname = true;
                  }
  
              }
  
  
              if (event.isEndElement()) {
                  EndElement element = (EndElement) event;
  
  
                  if (element.getName().toString().equalsIgnoreCase("displayName")) {
                      bname = false;
                  }
  
  
              }
  
  
              if (event.isCharacters()) {
                  // Depending upon the tag opened the data is retrieved .
                  Characters element = (Characters) event;
  
                  if (bname) {
                      System.out.println(element.getData());
                  }
  
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>XML:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;results
          xmlns="urn:www-collation-com:1.0"
          xmlns:coll="urn:www-collation-com:1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="urn:www-collation-com:1.0
                urn:www-collation-com:1.0/results.xsd"&gt;
  
      &lt;WebServiceImpl array="1"
          guid="FFVVRJ5618KJRHNFUIRV845NRUVHR" xsi:type="coll:com.model.topology.app.web.WebService"&gt;
          &lt;isPlaceholder&gt;false&lt;/isPlaceholder&gt;
          &lt;displayName&gt;server.servername1.siqom.siqom.us.com&lt;/displayName&gt;
          &lt;hierarchyType&gt;WebService&lt;/hierarchyType&gt;
          &lt;hierarchyDomain&gt;app.web&lt;/hierarchyDomain&gt;
      &lt;/WebServiceImpl&gt;
  &lt;/results&gt;
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;results
          xmlns="urn:www-collation-com:1.0"
          xmlns:coll="urn:www-collation-com:1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="urn:www-collation-com:1.0
                urn:www-collation-com:1.0/results.xsd"&gt;
  
      &lt;WebServiceImpl array="1"
          guid="FFVVRJ5618KJRHNFUIRV845NRUVHR" xsi:type="coll:com.model.topology.app.web.WebService"&gt;
          &lt;isPlaceholder&gt;false&lt;/isPlaceholder&gt;
          &lt;displayName&gt;server.servername2.siqom.siqom.us.com&lt;/displayName&gt;
          &lt;hierarchyType&gt;WebService&lt;/hierarchyType&gt;
          &lt;hierarchyDomain&gt;app.web&lt;/hierarchyDomain&gt;
      &lt;/WebServiceImpl&gt;
  &lt;/results&gt;
  
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;results
          xmlns="urn:www-collation-com:1.0"
          xmlns:coll="urn:www-collation-com:1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="urn:www-collation-com:1.0
                urn:www-collation-com:1.0/results.xsd"&gt;
  
      &lt;WebServiceImpl array="1"
          guid="FFVVRJ5618KJRHNFUIRV845NRUVHR" xsi:type="coll:com.model.topology.app.web.WebService"&gt;
          &lt;isPlaceholder&gt;false&lt;/isPlaceholder&gt;
          &lt;displayName&gt;server.servername3.siqom.siqom.us.com&lt;/displayName&gt;
          &lt;hierarchyType&gt;WebService&lt;/hierarchyType&gt;
          &lt;hierarchyDomain&gt;app.web&lt;/hierarchyDomain&gt;
      &lt;/WebServiceImpl&gt;
  &lt;/results&gt;
  
  
  etc...
  
  </code></pre>