title: Convert an XML file to CSV file using java
body: <p>I need help understanding the steps involved in converting an XML file into a CSV file using java.
  Here is an example of an XML file
  </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Sites&gt;
  &lt;Site id="101" name="NY-01" location="New York"&gt;
      &lt;Hosts&gt;
          &lt;Host id="1001"&gt;
             &lt;Host_Name&gt;srv001001&lt;/Host_Name&gt;
             &lt;IP_address&gt;10.1.2.3&lt;/IP_address&gt;
             &lt;OS&gt;Windows&lt;/OS&gt;
             &lt;Load_avg_1min&gt;1.3&lt;/Load_avg_1min&gt;
             &lt;Load_avg_5min&gt;2.5&lt;/Load_avg_5min&gt;
             &lt;Load_avg_15min&gt;1.2&lt;/Load_avg_15min&gt;
          &lt;/Host&gt;
          &lt;Host id="1002"&gt;
             &lt;Host_Name&gt;srv001002&lt;/Host_Name&gt;
             &lt;IP_address&gt;10.1.2.4&lt;/IP_address&gt;
             &lt;OS&gt;Linux&lt;/OS&gt;
             &lt;Load_avg_1min&gt;1.4&lt;/Load_avg_1min&gt;
             &lt;Load_avg_5min&gt;2.5&lt;/Load_avg_5min&gt;
             &lt;Load_avg_15min&gt;1.2&lt;/Load_avg_15min&gt;
          &lt;/Host&gt;
          &lt;Host id="1003"&gt;
             &lt;Host_Name&gt;srv001003&lt;/Host_Name&gt;
             &lt;IP_address&gt;10.1.2.5&lt;/IP_address&gt;
             &lt;OS&gt;Linux&lt;/OS&gt;
             &lt;Load_avg_1min&gt;3.3&lt;/Load_avg_1min&gt;
             &lt;Load_avg_5min&gt;1.6&lt;/Load_avg_5min&gt;
             &lt;Load_avg_15min&gt;1.8&lt;/Load_avg_15min&gt;
          &lt;/Host&gt;
          &lt;Host id="1004"&gt;
             &lt;Host_Name&gt;srv001004&lt;/Host_Name&gt;
             &lt;IP_address&gt;10.1.2.6&lt;/IP_address&gt;
             &lt;OS&gt;Linux&lt;/OS&gt;
             &lt;Load_avg_1min&gt;2.3&lt;/Load_avg_1min&gt;
             &lt;Load_avg_5min&gt;4.5&lt;/Load_avg_5min&gt;
             &lt;Load_avg_15min&gt;4.2&lt;/Load_avg_15min&gt;
          &lt;/Host&gt;     
      &lt;/Hosts&gt;
  &lt;/Site&gt;
  &lt;/Sites&gt;
  </code></pre>
  
  <p>and here is the resulting CSV file.</p>
  
  <pre><code>site_id, site_name, site_location, host_id, host_name, ip_address, operative_system, load_avg_1min, load_avg_5min, load_avg_15min
  101, NY-01, New York, 1001, srv001001, 10.1.2.3, Windows, 1.3, 2.5, 1.2
  101, NY-01, New York, 1002, srv001002, 10.1.2.4, Linux, 1.4, 2.5, 1.2
  101, NY-01, New York, 1003, srv001003, 10.1.2.5, Linux, 3.3, 1.6, 1.8
  101, NY-01, New York, 1004, srv001004, 10.1.2.6, Linux, 2.3, 4.5, 4.2
  </code></pre>
  
  <p>I was thinking of using a DOM parser to read the xml file. The problem I have with that is I would need to specify specific elements in to code by name, but I want it to be able to parse it without doing that.</p>
  
  <p>Are there any tools or libraries in java that would be able to help me achieve this.</p>
  
  <p>If I have a XML file of this format below and want to add the value of the InitgPty in the same row with MSgId (Pls note :InitgPty is in the next tag level, so it prints the value in the next row)</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;CstmrCdtTrfInitn&gt;
  &lt;GrpHdr&gt;
  &lt;MsgId&gt;XYZ07/ABC&lt;/MsgId&gt;
  &lt;NbOfTxs&gt;100000&lt;/NbOfTxs&gt;
  &lt;InitgPty&gt;
  &lt;Nm&gt;XYZ&lt;/Nm&gt;
  &lt;/InitgPty&gt;
  </code></pre>