Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7009109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:49:16+00:00 2026-05-27T21:49:16+00:00

There’s an XML stream which I need to parse. Since I only need to

  • 0

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).

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:

<players>
  <player>
    <id></id>
    <name></name>
    <teams total="2">
      <team>
        <id></id>
        <name></name>
        <start-date>
          <year>2009</year>
          <month>9</month>
        </start-date>
        <is-current>true</is-current>
      </team>
      <team>
        <id></id>
        <name></name>
        <start-date>
          <year>2007</year>
          <month>11</month>
        </start-date>
        <end-date>
          <year>2009</year>
          <month>7</month>
        </end-date>
      </team>
    </teams>
  </player>
</players>

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.

Here’s how my code looks like:

public class MyParser extends DefaultHandler {

    private String currentValue;
    private boolean inTeamsSection = false;
    private Player player;
    private Team team;
    private List<Team> 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<Team>();
        }
        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;
        }
    }
}

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.

What should I do differently? How can I know what a name tag, for instance, belongs to?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:49:17+00:00Added an answer on May 27, 2026 at 9:49 pm

    There is one neat trick when writing a SAX parser: It is allowed to change the
    ContentHandler of a XMLReader while parsing. This allows to separate the
    parsing logic for different elements into multiple classes, which makes the
    parsing more modular and reusable. When one handler sees its end element it
    switches back to its parent. How many handlers you implement would be left to
    you. The code would look like this:

    public class RootHandler extends DefaultHandler {
        private XMLReader reader;
        private List<Team> teams;
    
        public RootHandler(XMLReader reader) {
            this.reader = reader;
            this.teams = new LinkedList<Team>();
        }
    
        public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
            if (name.equals("team")) {
                // Switch handler to parse the team element
                reader.setContentHandler(new TeamHandler(reader, this));
            }
        }
    }
    
    public class TeamHandler extends DefaultHandler {
        private XMLReader reader;
        private RootHandler parent;
        private Team team;
        private StringBuilder content;
    
        public TeamHandler(XMLReader reader, RootHandler parent) {
            this.reader = reader;
            this.parent = parent;
            this.content = new StringBuilder();
            this.team = new Team();
        }
    
        // characters can be called multiple times per element so aggregate the content in a StringBuilder
        public void characters(char[] ch, int start, int length) throws SAXException {
            content.append(ch, start, length);
        }
    
        public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
            content.setLength(0);
        }
    
        public void endElement(String uri, String localName, String name) throws SAXException {
            if (name.equals("name")) {
                team.setName(content.toString());
            } else if (name.equals("team")) {
                parent.addTeam(team);
                // Switch handler back to our parent
                reader.setContentHandler(parent);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There are numerous Agile software development methods. Which ones have you used in practice
There are several types of objects in a system, and each has it's own
There are several shading languages available today like GLSL, HLSL, CG, which one to
There's a bug in FireBug: I accidentally clicked where the line numbers are, which
There was a smbmrx sample code using RDBSS in WDK Vista. But since WDK
There can only be one IDENTITY column per table Why is it so? Take
There is an application written in PHP which I am converting to Ruby. When
There are many free online services which provides you with large spaces to store
There are few apps like Strava which records users movements using GPS. It also
There is a div (form) which opens on the click of a button. There

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.