Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This solution does not need any CSV or XML libraries and, I know, it does not handle any illegal characters and encoding issues, but you might be interested in it as well, provided your CSV input does not break the above mentioned rules.</p> <p><strong>Attention:</strong> You should not use this code unless you know what you do or don't have the chance to use a further library (possible in some bureaucratic projects)... Use a StringBuffer for older Runtime Environments...</p> <p>So here we go:</p> <pre><code>BufferedReader reader = new BufferedReader(new InputStreamReader( Csv2Xml.class.getResourceAsStream("test.csv"))); StringBuilder xml = new StringBuilder(); String lineBreak = System.getProperty("line.separator"); String line = null; List&lt;String&gt; headers = new ArrayList&lt;String&gt;(); boolean isHeader = true; int count = 0; int entryCount = 1; xml.append("&lt;root&gt;"); xml.append(lineBreak); while ((line = reader.readLine()) != null) { StringTokenizer tokenizer = new StringTokenizer(line, ","); if (isHeader) { isHeader = false; while (tokenizer.hasMoreTokens()) { headers.add(tokenizer.nextToken()); } } else { count = 0; xml.append("\t&lt;entry id=\""); xml.append(entryCount); xml.append("\"&gt;"); xml.append(lineBreak); while (tokenizer.hasMoreTokens()) { xml.append("\t\t&lt;"); xml.append(headers.get(count)); xml.append("&gt;"); xml.append(tokenizer.nextToken()); xml.append("&lt;/"); xml.append(headers.get(count)); xml.append("&gt;"); xml.append(lineBreak); count++; } xml.append("\t&lt;/entry&gt;"); xml.append(lineBreak); entryCount++; } } xml.append("&lt;/root&gt;"); System.out.println(xml.toString()); </code></pre> <p>The input test.csv (stolen from another answer on this page):</p> <pre><code>string,float1,float2,integer hello world,1.0,3.3,4 goodbye world,1e9,-3.3,45 hello again,-1,23.33,456 hello world 3,1.40,34.83,4999 hello 2 world,9981.05,43.33,444 </code></pre> <p>The resulting output:</p> <pre><code>&lt;root&gt; &lt;entry id="1"&gt; &lt;string&gt;hello world&lt;/string&gt; &lt;float1&gt;1.0&lt;/float1&gt; &lt;float2&gt;3.3&lt;/float2&gt; &lt;integer&gt;4&lt;/integer&gt; &lt;/entry&gt; &lt;entry id="2"&gt; &lt;string&gt;goodbye world&lt;/string&gt; &lt;float1&gt;1e9&lt;/float1&gt; &lt;float2&gt;-3.3&lt;/float2&gt; &lt;integer&gt;45&lt;/integer&gt; &lt;/entry&gt; &lt;entry id="3"&gt; &lt;string&gt;hello again&lt;/string&gt; &lt;float1&gt;-1&lt;/float1&gt; &lt;float2&gt;23.33&lt;/float2&gt; &lt;integer&gt;456&lt;/integer&gt; &lt;/entry&gt; &lt;entry id="4"&gt; &lt;string&gt;hello world 3&lt;/string&gt; &lt;float1&gt;1.40&lt;/float1&gt; &lt;float2&gt;34.83&lt;/float2&gt; &lt;integer&gt;4999&lt;/integer&gt; &lt;/entry&gt; &lt;entry id="5"&gt; &lt;string&gt;hello 2 world&lt;/string&gt; &lt;float1&gt;9981.05&lt;/float1&gt; &lt;float2&gt;43.33&lt;/float2&gt; &lt;integer&gt;444&lt;/integer&gt; &lt;/entry&gt; &lt;/root&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload