I have a CSV file and looking to convert it to an XML. Im have a simple program that can read the first line of CSV but I haven't been able to put a loop in, to read the whole csv file, without getting errors any help on where to put in the loop would be much appreciated. I thought a while loops just before the creation of the child element. Once again any help would be much appreciated.
public static void main(String[] args) throws Exception {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document newDoc = domBuilder.newDocument();
Element rootElement = newDoc.createElement("parentelement");
newDoc.appendChild(rootElement);
BufferedReader csvReader = new BufferedReader(new FileReader("name.csv"));
String curLine = csvReader.readLine();
String[] csvFields = curLine.split(",");
Element rowElement = newDoc.createElement("childelement");
for(String value: csvFields){
Element curElement = newDoc.createElement(value);
curElement.appendChild(newDoc.createTextNode(value));
rowElement.appendChild(curElement);
rootElement.appendChild(rowElement);
}
csvReader.close();
TransformerFactory tranFactory = TransformerFactory.newInstance();
Transformer aTransformer = tranFactory.newTransformer();
Source src = new DOMSource(newDoc);
Result dest = new StreamResult(new File("output.xml"));
aTransformer.transform(src, dest);
}
