title: Why am I getting duplicate records from this method when I parse XML?
body: <p>I'm trying to parse content from XML using Stax. To create an object from it and place it in the list. I created the <code>parseXml()</code> method for that but if the element country has more than two element users, it always duplicates the last one.</p>
  <p>The method I used is this one:</p>
  <pre><code>public static List&lt;User&gt; parseXML() {
      List&lt;User&gt; empList = new ArrayList&lt;&gt;();
      User emp = null;
      XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
      try {
          XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(&quot;C:\\Users\\HP\\Desktop\\untitled1\\src\\main\\xml\\users.xml&quot;));
          while(xmlEventReader.hasNext()){
              XMLEvent xmlEvent = xmlEventReader.nextEvent();
              if (xmlEvent.isStartElement()){
                  StartElement startElement = xmlEvent.asStartElement();
                  if(startElement.getName().getLocalPart().equals(&quot;country&quot;)){
                      emp = new User();
                      //Get the 'id' attribute from Employee element
                      Attribute idAttr = startElement.getAttributeByName(new QName(&quot;name&quot;));
                      if(idAttr != null){
                          emp.setCountry(idAttr.getValue());
                      }
                  }
                  //set the other varibles from xml elements
                  else if(startElement.getName().getLocalPart().equals(&quot;first_name&quot;)){
                      xmlEvent = xmlEventReader.nextEvent();
                      emp.setFirst_name(xmlEvent.asCharacters().getData());
                  }else if(startElement.getName().getLocalPart().equals(&quot;last_name&quot;)){
                      xmlEvent = xmlEventReader.nextEvent();
                      emp.setLast_name(xmlEvent.asCharacters().getData());
                  }else if(startElement.getName().getLocalPart().equals(&quot;address&quot;)){
                      xmlEvent = xmlEventReader.nextEvent();
                      emp.setAddress(xmlEvent.asCharacters().getData());
                  }else if(startElement.getName().getLocalPart().equals(&quot;city&quot;)){
                      xmlEvent = xmlEventReader.nextEvent();
                      emp.setCity(xmlEvent.asCharacters().getData());
                  }else if(startElement.getName().getLocalPart().equals(&quot;email&quot;)){
                      xmlEvent = xmlEventReader.nextEvent();
                      emp.setEmail(xmlEvent.asCharacters().getData());
                  }
              }
              //if Employee end element is reached, add employee object to list
              if(xmlEvent.isEndElement()){
                  EndElement endElement = xmlEvent.asEndElement();
                  if(endElement.getName().getLocalPart().equals(&quot;user&quot;)){
                      empList.add(emp);
                  }
              }
          }
      } catch (FileNotFoundException | XMLStreamException e) {
          e.printStackTrace();
      }
      return empList;
  }
  </code></pre>
  <p>Here is XML:</p>
  <pre><code>&lt;data&gt;
      &lt;continent name=&quot;Europe&quot;&gt;
          &lt;country name=&quot;United Kingdom&quot;&gt;
              &lt;!-- Users --&gt;
              &lt;user&gt;
                  &lt;first_name&gt;John&lt;/first_name&gt;
                  &lt;last_name&gt;Smith&lt;/last_name&gt;
                  &lt;address&gt;320 Regent Street&lt;/address&gt;
                  &lt;city&gt;London&lt;/city&gt;
                  &lt;email&gt;john.smith@gmail.com&lt;/email&gt;
                  &lt;password&gt;skek3E5k&lt;/password&gt;
              &lt;/user&gt;
              &lt;user&gt;
                  &lt;first_name&gt;Michael&lt;/first_name&gt;
                  &lt;last_name&gt;Smith&lt;/last_name&gt;
                  &lt;address&gt;322 Regent Street&lt;/address&gt;
                  &lt;city&gt;London&lt;/city&gt;
                  &lt;email&gt;michael.smith@hotmail.com&lt;/email&gt;
                  &lt;password&gt;gk3288skW&lt;/password&gt;
              &lt;/user&gt;
          &lt;/country&gt;
          &lt;country name=&quot;Germany&quot;&gt;
              &lt;!-- Users --&gt;
              &lt;user&gt;
                  &lt;first_name&gt;Max&lt;/first_name&gt;
                  &lt;last_name&gt;Mustermann&lt;/last_name&gt;
                  &lt;address&gt;Berlinerstrasse 7&lt;/address&gt;
                  &lt;city&gt;Hamburg&lt;/city&gt;
                  &lt;email&gt;mmustermann@yahoo.com&lt;/email&gt;
                  &lt;password&gt;kdkei44229&lt;/password&gt;
              &lt;/user&gt;
            &lt;/country&gt;
      &lt;/continent&gt;
      &lt;continent name=&quot;North America&quot;&gt;
          &lt;country name=&quot;United States&quot;&gt;
              &lt;!-- Users --&gt;
              &lt;user&gt;
                  &lt;first_name&gt;Julia&lt;/first_name&gt;
                  &lt;last_name&gt;Johnson&lt;/last_name&gt;
                  &lt;address&gt;300 Boylston Ave E&lt;/address&gt;
                  &lt;city&gt;Seattle&lt;/city&gt;
                  &lt;email&gt;julia.johnson@gmail.com&lt;/email&gt;
                  &lt;password&gt;49dksSSe3&lt;/password&gt;
              &lt;/user&gt;
          &lt;/country&gt;
      &lt;/continent&gt;
  &lt;/data&gt;
  </code></pre>
  <p>What am I doing wrong in my method?</p>