用Java将.csv文件解析为xml

用Java将.csv文件解析为xml,java,xml,csv,Java,Xml,Csv,大家好,我正在尝试制作一个程序,将.csv文件中的数据转换为.csv文件,构建.csv文件的DOM表示,然后将其输出到xml文件 我现在正在使用下面的代码解析.csv文件,但我不确定如何获取这些信息并构建DOM表示。我读了很多书,偶然发现了“jaxb”,但似乎 .csv文件信息 QUESTIONS,Comment,Yes,Comment,No,Comment,Sit,Comment,Stand,Comment,Blank,Comment,Optional,Comment Is there fre

大家好,我正在尝试制作一个程序,将.csv文件中的数据转换为.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来实现这一点。有一种方法可以读取CSV文件(使用定义的模式-在本例中,您可以使用标题行),然后将其发送回XML(另一种自由行为)

可以使用以下方式读取CSV:

CsvSchema bootstrap = CsvSchema.emptySchema().withHeader();
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader(Map.class).with(bootstrap).readValues(file);
当然,还有很多其他的选择。至少要使用CSV解析器,例如,不要试图手动解析CSV(存在太多潜在问题,其中大多数问题已经得到有力解决)。

可能重复:
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(mappingIterator.next....