title: Hierarchy & data structure in Java (or XML to Object conversion: best practices)
body: <p>The problem:
  Let's say there is an XML file that contains both the data and the hierarchy of certain elements of interest to the application: </p>
  
  <pre><code>  &lt;root&gt;
      &lt;node title="lvl1Node"&gt;
          &lt;node title="lvl2Node"&gt;
              &lt;node title="lvl3Node"&gt;&lt;/node&gt;
          &lt;/node&gt;
      &lt;/node&gt;
      &lt;node title="lvl1Node2"&gt;&lt;/node&gt;
      &lt;node title="lvl1Node3"&gt;
          &lt;node title="lvl2Node2"&gt;
              &lt;node title="lvl3Node2"&gt;
                  &lt;node title="lvl4Node"&gt;&lt;/node&gt;
              &lt;/node&gt;
          &lt;/node&gt;
      &lt;/node&gt;
    &lt;/root&gt;
  </code></pre>
  
  <p>Now let's say your application needs to provide an API to retrieve these nodes. You need to write a method that returns the nodes without losing information about their hierarchy.</p>
  
  <p>My question is how would you go about it ? What kind of data type would you use. 
  A tree data type is the obvious answer but it not provided in the standard Collections API and writing it yourself is always a last resort (programmers are lazy, reinventing the wheel etc).</p>
  
  <p>I also thought of an ArrayList where each item is either an Object (for node without subnodes) or an Arraylist (for node with subnodes) but I like generics and this feels too much like a hack.
  Is there a cleverer way to do it ? </p>