title: Conversion of CSV to XML with JAVA
body: <p>I have a set of CSV data to be converted to XML. The codes look OK but the output isn't perfect enough. It omits some columns because they have no value and produces a long line of XML data instead of breaking it.</p>
  
  <p>This is a sample of my CSV data:</p>
  
  <pre class="lang-none prettyprint-override"><code>Name  Age Sex
  chi   23   
  kay   19  male
  John      male
  </code></pre>
  
  <p>And my code:</p>
  
  <pre><code>public class XMLCreators {
    // Protected Properties
    protected DocumentBuilderFactory domFactory = null;
    protected DocumentBuilder domBuilder = null;
  
    public XMLCreators() {
      try {
        domFactory = DocumentBuilderFactory.newInstance();
        domBuilder = domFactory.newDocumentBuilder();
      } catch (FactoryConfigurationError exp) {
        System.err.println(exp.toString());
      } catch (ParserConfigurationException exp) {
        System.err.println(exp.toString());
      } catch (Exception exp) {
        System.err.println(exp.toString());
      }
  
    }
  
    public int convertFile(String csvFileName, String xmlFileName,
        String delimiter) {
  
      int rowsCount = -1;
      try {
        Document newDoc = domBuilder.newDocument();
        // Root element
        Element rootElement = newDoc.createElement("XMLCreators");
        newDoc.appendChild(rootElement);
        // Read csv file
        BufferedReader csvReader;
        csvReader = new BufferedReader(new FileReader(csvFileName));
        int fieldCount = 0;
        String[] csvFields = null;
        StringTokenizer stringTokenizer = null;
  
        // Assumes the first line in CSV file is column/field names
        // The column names are used to name the elements in the XML file,
        // avoid the use of Space or other characters not suitable for XML element
        // naming
  
        String curLine = csvReader.readLine();
        if (curLine != null) {
          // how about other form of csv files?
          stringTokenizer = new StringTokenizer(curLine, delimiter);
          fieldCount = stringTokenizer.countTokens();
          if (fieldCount &gt; 0) {
            csvFields = new String[fieldCount];
            int i = 0;
            while (stringTokenizer.hasMoreElements())
              csvFields[i++] = String.valueOf(stringTokenizer.nextElement());
          }
        }
  
        // At this point the coulmns are known, now read data by lines
        while ((curLine = csvReader.readLine()) != null) {
          stringTokenizer = new StringTokenizer(curLine, delimiter);
          fieldCount = stringTokenizer.countTokens();
          if (fieldCount &gt; 0) {
            Element rowElement = newDoc.createElement("row");
            int i = 0;
            while (stringTokenizer.hasMoreElements()) {
              try {
                String curValue = String.valueOf(stringTokenizer.nextElement());
                Element curElement = newDoc.createElement(csvFields[i++]);
                curElement.appendChild(newDoc.createTextNode(curValue));
                rowElement.appendChild(curElement);
              } catch (Exception exp) {
              }
            }
            rootElement.appendChild(rowElement);
            rowsCount++;
          }
        }
        csvReader.close();
  
        // Save the document to the disk file
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();
        Source src = new DOMSource(newDoc);
        Result result = new StreamResult(new File(xmlFileName));
        aTransformer.transform(src, result);
        rowsCount++;
  
        // Output to console for testing
        // Resultt result = new StreamResult(System.out);
  
      } catch (IOException exp) {
        System.err.println(exp.toString());
      } catch (Exception exp) {
        System.err.println(exp.toString());
      }
      return rowsCount;
      // "XLM Document has been created" + rowsCount;
    }
  }
  </code></pre>
  
  <p>When this code is executed on the above data it produces:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;XMLCreators&gt;
  &lt;row&gt;
  &lt;Name&gt;chi&lt;/Name&gt;
  &lt;Age&gt;23&lt;/Age&gt;
  &lt;/row&gt;
  &lt;row&gt;
  &lt;Name&gt;kay&lt;/Name&gt;
  &lt;Age&gt;19&lt;/Age&gt;
  &lt;sex&gt;male&lt;/sex&gt;
  &lt;/row&gt;
  &lt;row&gt;
  &lt;Name&gt;john&lt;/Name&gt;
  &lt;Age&gt;male&lt;/Age&gt;
  &lt;/row&gt;
  &lt;/XMLCreators&gt;
  </code></pre>
  
  <p>I arranged it in this form myself but the output produces a long line. The output to be produced should be:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;XMLCreators&gt;
  &lt;row&gt;
  &lt;Name&gt;chi&lt;/Name&gt;
  &lt;Age&gt;23&lt;/Age&gt;
  &lt;sex&gt;&lt;/sex&gt;
  &lt;/row&gt;
  &lt;row&gt;
  &lt;Name&gt;kay&lt;/Name&gt;
  &lt;Age&gt;19&lt;/Age&gt;
  &lt;sex&gt;male&lt;/sex&gt;
  &lt;/row&gt;
  &lt;row&gt;
  &lt;Name&gt;john&lt;/Name&gt;
  &lt;Age&gt;&lt;/Age&gt;
  &lt;sex&gt;male&lt;/sex&gt;
   &lt;/row&gt;
   &lt;/XMLCreators&gt;
  </code></pre>