title: Getting Parent Child Hierarchy in Sax XML parser
body: <p>I'm using SAX (Simple API for XML) to parse an XML document. I'm getting output for all the tags the file have, but i want it to show the tags in parent child hierarchy. 
  For Example:
  This is my output</p>
  
  <pre><code>&lt;dblp&gt;
  &lt;www&gt;
  &lt;author&gt;
  &lt;/author&gt;&lt;title&gt;
  &lt;/title&gt;&lt;url&gt;
  &lt;/url&gt;&lt;year&gt;
  &lt;/year&gt;&lt;/www&gt;&lt;inproceedings&gt;
  &lt;month&gt;
  &lt;/month&gt;&lt;pages&gt;
  &lt;/pages&gt;&lt;booktitle&gt;
  &lt;/booktitle&gt;&lt;note&gt;
  &lt;/note&gt;&lt;cdrom&gt;
  &lt;/cdrom&gt;&lt;/inproceedings&gt;&lt;article&gt;
  &lt;journal&gt;
  &lt;/journal&gt;&lt;volume&gt;
  &lt;/volume&gt;&lt;/article&gt;&lt;ee&gt;
  &lt;/ee&gt;&lt;book&gt;
  &lt;publisher&gt;
  &lt;/publisher&gt;&lt;isbn&gt;
  &lt;/isbn&gt;&lt;/book&gt;&lt;incollection&gt;
  &lt;crossref&gt;
  &lt;/crossref&gt;&lt;/incollection&gt;&lt;editor&gt;
  &lt;/editor&gt;&lt;series&gt;
  &lt;/series&gt;&lt;/dblp&gt;
  </code></pre>
  
  <p>But i want it to display the output like this (it displays the children with extra spacing (that's how i want it to be))</p>
  
  <pre><code>&lt;dblp&gt;
    &lt;www&gt;
      &lt;author&gt;
      &lt;/author&gt;
      &lt;title&gt;
      &lt;/title&gt;
      &lt;url&gt;
      &lt;/url&gt;
      &lt;year&gt;
      &lt;/year&gt;
    &lt;/www&gt;
    &lt;inproceedings&gt;
      &lt;month&gt;
      &lt;/month&gt;
      &lt;pages&gt;
      &lt;/pages&gt;
      &lt;booktitle&gt;
      &lt;/booktitle&gt;
      &lt;note&gt;
      &lt;/note&gt;
      &lt;cdrom&gt;
      &lt;/cdrom&gt;
    &lt;/inproceedings&gt;
    &lt;article&gt;
      &lt;journal&gt;
      &lt;/journal&gt;
      &lt;volume&gt;
      &lt;/volume&gt;
    &lt;/article&gt;
    &lt;ee&gt;
    &lt;/ee&gt;
    &lt;book&gt;
      &lt;publisher&gt;
      &lt;/publisher&gt;
      &lt;isbn&gt;
      &lt;/isbn&gt;
    &lt;/book&gt;
    &lt;incollection&gt;
      &lt;crossref&gt;
      &lt;/crossref&gt;
    &lt;/incollection&gt;
    &lt;editor&gt;
    &lt;/editor&gt;
    &lt;series&gt;
    &lt;/series&gt;
  &lt;/dblp&gt;
  </code></pre>
  
  <p>But i can't figure out how can i detect that parser is parsing a parent tag or a children. </p>
  
  <p>here is my code:</p>
  
  <pre><code>package com.teamincredibles.sax;
  
  import javax.xml.parsers.SAXParser;
  import javax.xml.parsers.SAXParserFactory;
  
  import org.xml.sax.Attributes;
  import org.xml.sax.SAXException;
  import org.xml.sax.helpers.DefaultHandler;
  
  public class Parser extends DefaultHandler {
  
    public void getXml() {
      try {
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();
        final MySet openingTagList = new MySet();
        final MySet closingTagList = new MySet();
        DefaultHandler defaultHandler = new DefaultHandler() {
  
          public void startDocument() throws SAXException {
            System.out.println("Starting Parsing...\n");
          }
  
          public void endDocument() throws SAXException {
            System.out.print("\n\nDone Parsing!");
          }
  
          public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
            if (!openingTagList.contains(qName)) {
              openingTagList.add(qName);
              System.out.print("&lt;" + qName + "&gt;\n");
            }
          }
  
          public void characters(char ch[], int start, int length)
          throws SAXException {
            /*for(int i=start; i&lt;(start+length);i++){
              System.out.print(ch[i]);
          }*/
          }
  
          public void endElement(String uri, String localName, String qName)
          throws SAXException {
            if (!closingTagList.contains(qName)) {
              closingTagList.add(qName);
              System.out.print("&lt;/" + qName + "&gt;");
            }
          }
        };
  
        saxParser.parse("xml/sample.xml", defaultHandler);
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  
    public static void main(String args[]) {
      Parser readXml = new Parser();
      readXml.getXml();
    }
  }
  </code></pre>