簡體   English   中英

使用Java將.csv文件解析為xml

[英]Parsing a .csv file to xml with Java

大家好,我正在嘗試制作一個程序來轉換.csv文件中的數據,構建.csv文件的DOM表示形式,然后將其輸出到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]

也許一個簡單的低代碼選擇是為此使用Jackson。 有一個Jackson CSV模塊 ,它將讀取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(存在太多潛在問題,其中大多數問題已經得到了有效解決)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM