Convert csv to xml in java
here i have used opencsv-2.2.jar library for converting csv file into xml.
CsvToXmlConvert.java
package com.convert;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import au.com.bytecode.opencsv.CSVReader;
public class CsvToXmlConvert {
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
StringBuilder xml = new StringBuilder();
String startFile = "/Book1.csv";
try {
File file=new File(request.getRealPath(startFile));
CSVReader reader = new CSVReader(new FileReader(file));
String[] line = null;
String[] header = reader.readNext();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Result");
doc.appendChild(rootElement);
Element Employees = doc.createElement("Employees");
while((line = reader.readNext())!=null){
Element Employee = doc.createElement("Employee");
for (int i = 0; i < header.length; i++) {
Element elm = doc.createElement(header[i].trim());
elm.appendChild(doc.createTextNode(line[i].trim()));
Employee.appendChild(elm);
}
Employees.appendChild(Employee);
}
rootElement.appendChild(Employees);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/home/file.xml"));
transformer.transform(source, result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
CSV FILE FORMATE
Book1.csv
FirstName, LastName, Number
Pushpak, Khatri, 9999999999
Raj,Patel, 9999999999
Simran, Khanna, 9999999999
here i have used opencsv-2.2.jar library for converting csv file into xml.
CsvToXmlConvert.java
package com.convert;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import au.com.bytecode.opencsv.CSVReader;
public class CsvToXmlConvert {
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
StringBuilder xml = new StringBuilder();
String startFile = "/Book1.csv";
try {
File file=new File(request.getRealPath(startFile));
CSVReader reader = new CSVReader(new FileReader(file));
String[] line = null;
String[] header = reader.readNext();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Result");
doc.appendChild(rootElement);
Element Employees = doc.createElement("Employees");
while((line = reader.readNext())!=null){
Element Employee = doc.createElement("Employee");
for (int i = 0; i < header.length; i++) {
Element elm = doc.createElement(header[i].trim());
elm.appendChild(doc.createTextNode(line[i].trim()));
Employee.appendChild(elm);
}
Employees.appendChild(Employee);
}
rootElement.appendChild(Employees);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/home/file.xml"));
transformer.transform(source, result);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
CSV FILE FORMATE
Book1.csv
FirstName, LastName, Number
Pushpak, Khatri, 9999999999
Raj,Patel, 9999999999
Simran, Khanna, 9999999999
No comments:
Post a Comment