title: Java SAX Parsing
body: <p>There's an XML stream which I need to parse. Since I only need to do it once and build my java objects, SAX looks like the natural choice. I'm extending DefaultHandler and implementing the startElement, endElement and characters methods, having members in my class where I save the current read value (taken in the characters method).</p>
  
  <p>I have no problem doing what I need, but my code got quite complex and I'm sure there's no reason for that and that I can do things differently.
  The structure of my XML is something like this:</p>
  
  <pre><code>&lt;players&gt;
    &lt;player&gt;
      &lt;id&gt;&lt;/id&gt;
      &lt;name&gt;&lt;/name&gt;
      &lt;teams total="2"&gt;
        &lt;team&gt;
          &lt;id&gt;&lt;/id&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;start-date&gt;
            &lt;year&gt;2009&lt;/year&gt;
            &lt;month&gt;9&lt;/month&gt;
          &lt;/start-date&gt;
          &lt;is-current&gt;true&lt;/is-current&gt;
        &lt;/team&gt;
        &lt;team&gt;
          &lt;id&gt;&lt;/id&gt;
          &lt;name&gt;&lt;/name&gt;
          &lt;start-date&gt;
            &lt;year&gt;2007&lt;/year&gt;
            &lt;month&gt;11&lt;/month&gt;
          &lt;/start-date&gt;
          &lt;end-date&gt;
            &lt;year&gt;2009&lt;/year&gt;
            &lt;month&gt;7&lt;/month&gt;
          &lt;/end-date&gt;
        &lt;/team&gt;
      &lt;/teams&gt;
    &lt;/player&gt;
  &lt;/players&gt;
  </code></pre>
  
  <p>My problem started when I realized that the same tag names are used in several areas of the file. For example, id and name exist for both a player and a team. I want to create instances of my java classes Player and Team. While parsing, I kept boolean flags telling me whether I'm in the teams section so that in the endElement I will know that the name is a team's name, not a player's name and so on.</p>
  
  <p>Here's how my code looks like:</p>
  
  <pre><code>public class MyParser extends DefaultHandler {
  
      private String currentValue;
      private boolean inTeamsSection = false;
      private Player player;
      private Team team;
      private List&lt;Team&gt; teams;
  
      public void characters(char[] ch, int start, int length) throws SAXException {
          currentValue = new String(ch, start, length);
      }
  
      public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
          if(name.equals("player")){
              player = new Player();
          }
          if (name.equals("teams")) {
              inTeamsSection = true;
              teams = new ArrayList&lt;Team&gt;();
          }
          if (name.equals("team")){
              team = new Team();
          }
      }   
  
      public void endElement(String uri, String localName, String name) throws SAXException {
          if (name.equals("id")) {
              if(inTeamsSection){
                  team.setId(currentValue);
              }
              else{
                  player.setId(currentValue);
              }
          }
          if (name.equals("name")){
              if(inTeamsSection){
                  team.setName(currentValue);
              }
              else{
                  player.setName(currentValue);
              }
          }
          if (name.equals("team")){
              teams.add(team);
          }
          if (name.equals("teams")){
              player.setTeams(teams);
              inTeamsSection = false;
          }
      }
  }
  </code></pre>
  
  <p>Since in my real scenario I have more nodes to a player in addition to the teams and those nodes also have tags like name and id, I found myself messed up with several booleans similar to the inTeamsSection and my endElement method becomes long and complex with many conditions.</p>
  
  <p>What should I do differently? How can I know what a name tag, for instance, belongs to?</p>
  
  <p>Thanks!</p>