java - 使用 Java 将 .csv 文件解析为 xml

标签 java xml 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 来实现此目的。有一个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(存在太多潜在问题,其中大部分已得到可靠解决)。

关于java - 使用 Java 将 .csv 文件解析为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43095220/

相关文章:

java - 数组的所有可能组合

java - Android - 如何在图片点击时切换 Activity

java - XML 类的多重继承

excel - 如何将具有可扩展行的 react 表导出到 csv 或 excel?

java - 使用无状态 Bean 和 DI 代替 POJO 和静态方法有哪些优点

java - 无法将 jTextfield 字符串转换为浮点值......给出数字格式异常

c# - 如何使用 Web 服务更新 Sharepoint 上上传文档的属性?

xml - xsl :fo - how to split for-each on pages

csv - hadoop如何读取输入文件?

excel - 没有指数数字的 CSV 文件创建