title: building a dom tree reading html using java
body: <p>I am trying to build the dom tree using the scanner class in java to read a give html. Below is my implementation using a stack. for some strange reason when i try to print my tree only the root is showing up and nothing else</p>
  
  <pre><code>public void build() {
  
          root = new TagNode("", null, null);
          TagNode ptr = null;
  
          Stack&lt;TagNode&gt; tags = new Stack&lt;TagNode&gt;();
  
          while (sc.hasNextLine()) {
  
              String tag = sc.nextLine();
  
              if (tag.equals("&lt;html&gt;")) {
  
                  ptr = new TagNode("html", null, null);
                  tags.push(ptr);
                  root.tag = "html";
  
              }
  
              else if (tag.charAt(0) == '&lt;') {
  
                  if (tag.charAt(1) == '/') {
  
                      tags.pop();
                      continue;
  
                  }
  
                  else if (tags.peek().firstChild == null) {
  
                      String temp = tag.replaceAll("&lt;", "");
                      temp = temp.replaceAll("&gt;", "");
                      ptr = new TagNode(temp, null, null);
                      tags.peek().firstChild = ptr;
                      tags.push(ptr);
  
                  }
  
                  else {
  
                      TagNode temp = tags.peek().firstChild;
  
                      while (temp.sibling != null) {
  
                          temp = temp.sibling;
  
                      }
  
                      String a = tag.replaceAll("&lt;", "");
                      a = a.replaceAll("&gt;", "");
                      ptr = new TagNode(a, null, null);
  
                      temp.sibling = ptr;
                      tags.push(ptr);
  
                  }
  
              }
  
              else {
  
                  if (tags.peek().firstChild == null) {
  
                      tags.peek().firstChild = new TagNode(tag, null, null);
  
                  }
  
                  else {
  
                      TagNode temp = tags.peek().firstChild;
  
                      while (temp.sibling != null) {
  
                          temp = temp.sibling;
  
                      }
  
                      temp.sibling = new TagNode(tag, null, null);
  
                  }
              }       
          }           
  
      }
  </code></pre>