title: SAX parsing and parent / child relations
body: <p>So I am facing this issue where I am required to parse an XML file to popluate domain objects. Sounds simple enough, however, an element within the XML file can have an unknown ammount of children, hence the domain object can have a instance variable to another object of the class, that object can have a variable to an instance variable of the same class and so on. To give you an example, this is a provided example for the XML file:</p>
  
  <pre><code>&lt;categories&gt;
      &lt;category id="1"&gt;
          &lt;name&gt;XML&lt;/name&gt;
          &lt;category id="2"&gt;
              &lt;name&gt;XPath&lt;/name&gt;
          &lt;/category&gt;
          &lt;category id="3"&gt;
              &lt;name&gt;XML Schema&lt;/name&gt;
          &lt;/category&gt;
          &lt;category id="4"&gt;
              &lt;name&gt;XSLT&lt;/name&gt;
          &lt;/category&gt;
          &lt;category id="5"&gt;
              &lt;name&gt;XSL-FO&lt;/name&gt;
          &lt;/category&gt;
          &lt;category id="6"&gt;
              &lt;name&gt;XQuery&lt;/name&gt;
          &lt;/category&gt;
      &lt;/category&gt;
      &lt;category id="7"&gt;
          &lt;name&gt;Java&lt;/name&gt;
          &lt;category id="100"&gt;
              &lt;name&gt;SDK&lt;/name&gt;
              &lt;category id="8"&gt;
                  &lt;name&gt;Collections&lt;/name&gt;
              &lt;/category&gt;
              &lt;category id="9"&gt;
                  &lt;name&gt;NIO&lt;/name&gt;
              &lt;/category&gt;
              &lt;category id="10"&gt;
                  &lt;name&gt;Concurrency&lt;/name&gt;
              &lt;/category&gt;
          &lt;/category&gt;
          &lt;category id="1000"&gt;
              &lt;name&gt;EE&lt;/name&gt;
              &lt;category id="11"&gt;
                  &lt;name&gt;EJB&lt;/name&gt;
              &lt;/category&gt;
              &lt;category id="12"&gt;
                  &lt;name&gt;Web&lt;/name&gt;
              &lt;/category&gt;
              &lt;category id="13"&gt;
                  &lt;name&gt;Webservices&lt;/name&gt;
              &lt;/category&gt;
          &lt;/category&gt;
          &lt;category id="0"&gt;
              &lt;name&gt;Examen boeken&lt;/name&gt;
          &lt;/category&gt;
      &lt;/category&gt;
  &lt;/categories&gt;
  </code></pre>
  
  <p>I have already done this with a DOM parser but for my study I am required to do this with a SAX parser aswell. I am getting stuck on the point where I need to tell which element has which elements as children and which element has which element as parent. </p>
  
  <p>As of now I managed to get all category entries in a map containing their ID and name. </p>
  
  <p>The code looks like this: </p>
  
  <pre><code>public static void main(String[] args) throws SAXException, IOException,
          ParserConfigurationException {
      Bookshelf mijnBookshelf = new Bookshelf("boekenfestijn");
      Map&lt;Integer, Category&gt; categories = new HashMap&lt;Integer, Category&gt;();
      // TODO inlezen
      try {
  
          SAXParserFactory factory = SAXParserFactory.newInstance();
          SAXParser saxParser = factory.newSAXParser();
  
          DefaultHandler handler = new DefaultHandler() {
  
              String reading = null;
  
              boolean inCategory = false;
              boolean inName = false;
  
              int categoryId;
  
              Category currentCategory;
  
  
              public void startElement(String uri, String localName,
                      String qName, Attributes attributes)
                      throws SAXException {
  
                  if(qName.equalsIgnoreCase("CATEGORY") &amp;&amp; attributes.getValue("id") != null){
                      inCategory = true;
                      categoryId = Integer.parseInt(attributes.getValue("id"));
                      System.out.println("START HANDLING ID -&gt; " + attributes.getValue("id"));
  
                  }
                  if(qName.equalsIgnoreCase("NAME")){
                      inName = true;
                  }
  
  
              }
  
              public void endElement(String uri, String localName,
                      String qName) throws SAXException {
  
  
                  if(inCategory){
                      inCategory = false;
                      System.out.println("CATEGORY ID : " + categoryId + " NAME : " + reading);
                      currentCategory = new Category(categoryId, reading);
                      currentCategory.setBookshelf(mijnBookshelf);
                      categories.put(categoryId, currentCategory);
                      System.out.println("END HANDLING");
                  }
  
                  if(inName){
                      inName = false;
                  }
  
  
  
              }
  
  
  
  
  
  
  
  
  
  
  
  
  
              public void characters(char ch[], int start, int length)
                      throws SAXException {
                  reading = new String(ch, start, length);
  
  
              }
  
          };
  
          saxParser.parse("bookshelf.xml", handler);
  
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      for (Integer i : categories.keySet()) {
          System.out.println("ID: " + categories.get(i).getId() + "-&gt;"
                  + categories.get(i).getName());
      }
  </code></pre>
  
  <p>And for the "category" class</p>
  
  <pre><code>public class Category {
  
  private Integer id;
  private String name;
  private Category parent;
  private List&lt;Category&gt; children = new ArrayList&lt;Category&gt;();
  private Bookshelf bookshelf;
  
  public Category(){
  
  }
  
  public Category(Integer id, String name) {
      super();
      this.id = id;
      this.name = name;
  }
  
  public Integer getId() {
      return id;
  }
  
  public void setId(Integer id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public Category getParent() {
      return parent;
  }
  
  public void setParent(Category parent){
      this.parent = parent;
  }
  
  public List&lt;Category&gt; getChildren() {
      return children;
  }
  
  public String toString() {
      String s = bookshelf.getName() + "/";
      if (parent != null) {
          s = parent.toString();
      }
      s += name + "/";
      return s;
  }
  
  public Bookshelf getBookshelf() {
      return bookshelf;
  }
  
  public void setBookshelf(Bookshelf bookshelf) {
      this.bookshelf = bookshelf;
  }
  </code></pre>
  
  <p>And this is the point at which I am getting stuck? how could I proceed to define the parent child relations? how would I know in any point in my handler which element has which element as children / parent?</p>
  
  <p>Any help would be greatly appreciated!</p>
  
  <p>TLDR: How can one define parent / child relations when using a sax parser to populate domain objects ?</p>