2017-03-29 108 views
0

大家好,我正在嘗試製作一個程序,用於轉換.csv文件中的數據,構建.csv文件的DOM表示,然後將其輸出到xml文件。用Java解析.csv文件到xml

我現在正在用下面的代碼解析.csv文件,但我不知道如何去獲取這些信息並構建DOM表示。我讀過周圍,跨越「JAXB」來了,但它似乎

.csv文件信息

QUESTIONS,Comment,Yes,Comment,No,Comment,Sit,Comment,Stand,Comment,Blank,Comment,Optional,Comment 
Is there free circulation of air through and about the building in which you work?,a,468,,249,,,,,,93,,, 
"Are there offensive odors in the rooms occupied by employees; if so, from what causes?",b,342,,233,,,,,,235,,, 
Are there facilities for washing?,c,460,,124,,,,,,226,,, 
"Are seats provided, as prescribed by law?",,320,,192,,,,,,298,,, 
Are employees furnished with pure drinking water?,d,527,,102,,,,,,181,,, 
Does the work require the employees to stoop over?,,587,,116,,,,,,107,,, 
Are there proper and separate facilities for change of dress by males and females?,e,354,,221,,,,,,235,,, 
Are there separate water-closets for males and females?,f,509,,126,g,,,,,175,,, 
Are there stairways from the windows outside?,,350,,318,,,,,,148,,, 
Are the rooms supplied with water pipes?,,265,,385,,,,,,165,,, 
Are there hose attachments?,,224,,375,,,,,,218,,, 
Are employees compelled to stand or sit at their work?,h,,,,,469,,175,,99,,67, 
Are there water tanks and buckets on each floor?,,236,,387,,,,,,198,,, 

解析器類。

import java.io.BufferedReader; 
import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import java.util.Arrays; 

public class parserClass { 

@SuppressWarnings("rawtypes") 
    public static void main(String[] args) throws Exception { 
       String splitBy = ","; 
     BufferedReader br = new BufferedReader(new FileReader("surveydata.csv")); 
     String line = br.readLine(); 
     while((line = br.readLine()) !=null){ 
      String[] b = line.split(splitBy); 
      System.out.println(Arrays.toString(b)); 
     } 
     br.close(); 

} 
}* 

它輸出到控制檯

[Is there free circulation of air through and about the building in which you work?, a, 468, , 249, , , , , , 93] 
["Are there offensive odors in the rooms occupied by employees; if so, from what causes?", b, 342, , 233, , , , , , 235] 
[Are there facilities for washing?, c, 460, , 124, , , , , , 226] 
["Are seats provided, as prescribed by law?", , 320, , 192, , , , , , 298] 
[Are employees furnished with pure drinking water?, d, 527, , 102, , , , , , 181] 
[Does the work require the employees to stoop over?, , 587, , 116, , , , , , 107] 
[Are there proper and separate facilities for change of dress by males and females?, e, 354, , 221, , , , , , 235] 
[Are there separate water-closets for males and females?, f, 509, , 126, g, , , , , 175] 
[Are there stairways from the windows outside?, , 350, , 318, , , , , , 148] 
[Are the rooms supplied with water pipes?, , 265, , 385, , , , , , 165] 
[Are there hose attachments?, , 224, , 375, , , , , , 218] 
[Are employees compelled to stand or sit at their work?, h, , , , , 469, , 175, , 99, , 67] 
[Are there water tanks and buckets on each floor?, , 236, , 387, , , , , , 198] 
+2

可能的重複:http://stackoverflow.com/questions/12120055/conversion-of-csv-to-xml-with-java –

回答

0

也許一個簡單的,低碼中的信息,選擇是使用傑克遜此。有一個Jackson CSV module將讀取CSV文件(具有定義的模式 - 在這種情況下,您可以使用標題行),然後將其發回XML(另一個自由行爲)。

人們可以使用閱讀CSV:

CsvSchema bootstrap = CsvSchema.emptySchema().withHeader(); 
CsvMapper csvMapper = new CsvMapper(); 
MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader(Map.class).with(bootstrap).readValues(file); 

,然後使用類似的發射爲XML:

XmlMapper xmlMapper = new XmlMapper(); 
xmlMapper.writeValue(mappingIterator.next.... 

有,當然,這樣做的許多其他選項。至少使用CSV解析器(如Apache Commons CSV),不要試圖手動解析CSV(有太多潛在問題,其中大部分已經得到了強有力的解決)。