title: How to recursively iterate through a recursive data structure
body: <p>I have a datastructure as follows:</p>
  
  <pre><code> public class A{
      String number;
      Map &lt;String ,B&gt; BMap;
   }
  
   public class B{
      String number;
      Map &lt;String ,A&gt; AMap;
   }
  </code></pre>
  
  <hr>
  
  <pre><code>0 B
      0.0 A
      0.1 A
          0.1.0 B
           ...so on
       1 B
        1.0 A
           1.0.0 B
      1.1 A
  </code></pre>
  
  <p>and so on. <strong>Note:</strong> No <code>B</code> comes after <code>B</code>.</p>
  
  <p>Now i want to iterate though the map of class <code>B</code>, i.e <code>AMap</code>. I am trying to create an XML-structure out of it. I want some attribute like id-number. The id-number at each level for <code>A</code> and <code>B</code> should be like we have structure in books. What is the most efficient way to do this?</p>