title: Convert XML file to CSV in java
body: <p>@Before There will be probably some duplicate questions suggestions, I don't think that is the case maybe read this first, I'll try to be as brief as possible. Title gives basic idea.</p>
  
  <p>Here is an example XML(case 1) :</p>
  
  <pre><code>&lt;root&gt;
        &lt;Item&gt;
          &lt;ItemID&gt;4504216603&lt;/ItemID&gt;
          &lt;ListingDetails&gt;
            &lt;StartTime&gt;10:00:10.000Z&lt;/StartTime&gt;
            &lt;EndTime&gt;10:00:30.000Z&lt;/EndTime&gt;
            &lt;ViewItemURL&gt;http://url&lt;/ViewItemURL&gt;
              ....
             &lt;/item&gt;      
  </code></pre>
  
  <p>Here is an example XML(case 2) :</p>
  
  <pre><code>          &lt;Item&gt;
              &lt;ItemID&gt;4504216604&lt;/ItemID&gt;
              &lt;ListingDetails&gt;
                &lt;StartTime&gt;10:30:10.000Z&lt;/StartTime&gt;
                &lt;!-- Start difference from case 1 --&gt;
                &lt;averages&gt;
                &lt;AverageTime&gt;value1&lt;/AverageTime&gt;
                &lt;category type="TX"&gt;9823&lt;/category&gt;
                &lt;category type="TY"&gt;9112&lt;/category&gt;
                &lt;AveragePrice&gt;value2&lt;/AveragePrice&gt;
                &lt;/averages&gt;
                &lt;!-- End difference from case 1 --&gt;
                &lt;EndTime&gt;11:00:10.000Z&lt;/EndTime&gt;
                &lt;ViewItemURL&gt;http://url&lt;/ViewItemURL&gt;
                  ....
                 &lt;/item&gt;
                  &lt;/root&gt;
  </code></pre>
  
  <p>I borrowed this XML from google, anyways my objects are not always the same, sometimes there are extra elements like in case2. Now I'd like to produce CSV like this from both cases:</p>
  
  <pre><code>ItemID,StartTime,EndTime,ViewItemURL,AverageTime,AveragePrice
  4504216603,10:00:10.000Z,10:00:30.000Z,http://url
  4504216604,10:30:10.000Z,11:00:10.000Z,http://url,value1,value2
  </code></pre>
  
  <p>This 1st line is header it should also be included in csv. I got some useful links to stax today, I don't really don't know what is the right/optimal approach for this, I'm struggling with this for 3 days now, not really willing to give up yet.</p>
  
  <p>Tell me what you think how would you solve this</p>
  
  <p><em>I forgot to mention this is very huge xml file up to 1gb</em></p>
  
  <p><strong>BOUNTY UPDATE :</strong></p>
  
  <p>I'm looking for more Generic approach, meaning that this should work for any number of nodes with any depth, and sometimes as in the example xml, it can happen that one <code>item</code> object has greater number of nodes than the next/previous one so there should be also case for that(so all columns and values match in CSV). </p>
  
  <p>Also it can happen that nodes have the same name/localName but different values and attributes, if that is the case then new column should appear in CSV with appropriate value. (I added example of this case inside <code>&lt;averages&gt;</code> tag called <code>category</code>)</p>