Reputation:
Hello all so i'm trying to make a program that converts data from a .csv file, build a DOM representation of the .csv file and then output it to an xml file.
I have the .csv file being parsed at the moment with the code below but i'm not sure how to go about taking this information and building a DOM representation. I've read around and came across 'jaxb' but it seem
The .csv file information
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,,,
The parser class.
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();
}
}*
The information it outputs to the console
[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]
Upvotes: 0
Views: 3198
Reputation: 1153
Perhaps a simple, low-code, option is to use Jackson for this. There is a Jackson CSV module that will read CSV files (with a defined schema - in this case you can use the header row), and then emit it back to XML (another free behaviour).
One can read the CSV using:
CsvSchema bootstrap = CsvSchema.emptySchema().withHeader();
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<?, ?>> mappingIterator = csvMapper.reader(Map.class).with(bootstrap).readValues(file);
and then emit it as XML using something like:
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.writeValue(mappingIterator.next....
There are, of course, many other options for doing this. At the very least use a CSV parser such as Apache Commons CSV and don't attempt to parse the CSV by hand (there are too many potential problems, most of which have been robustly solved already).
Upvotes: 1