2017-03-29 1 views
0

안녕하세요 .CSV 파일의 데이터를 변환하고 .csv 파일의 DOM 표현을 작성한 다음 xml 파일로 출력하는 프로그램을 만들려고합니다.Java로 .csv 파일 구문 분석

아래 코드를 사용하여 .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 (다른 무료 동작)으로 내 보냅니다.

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

과 같은 것을 사용하여 XML로 방출 후 :

하나는 사용하여 CSV를 읽을 수

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

이 있으며,이 작업을 수행하는 물론, 다른 많은 옵션을 제공합니다. 최소한 Apache Commons CSV과 같은 CSV 파서를 사용하고 수작업으로 CSV를 구문 분석하지 마십시오 (잠재적으로 너무 많은 문제가 있으며 대부분은 이미 강력하게 해결되었습니다).