How to read the data from an .csv file and write into an xml file using java? (test_sample.csv(it should contain at least 39 lines)) i tried this plz check it.
package Test2; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.StringTokenizer; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; public class WriteXMLFile { public static void main(String argv[]) throws IOException { try{ FileReader fr= new FileReader("C:\\Documents and Settings/janardhany/Desktop/test_sample.txt") ; BufferedReader br = new BufferedReader(fr); // FileInputStream fStream = new FileInputStream("C:\\Documents and Settings/janardhany/Desktop/test_sample.xls"); // DataInputStream br= new DataInputStream(fStream); String s=null; String s2=null; // while((s=br.readLine())!=null ){ // String in = br.readLine(); // String f[] = in.split(","); // // StringTokenizer st = new StringTokenizer(s, "\t"); // // while(st.hasMoreTokens()) { // // // System.out.println( st.nextToken()); // // } // // } // // // s2=st.nextToken(); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootElement = doc.createElement("ProductCatalogue"); doc.appendChild(rootElement); while((s=br.readLine())!=null ){ String in = br.readLine(); String f[] = in.split(","); for(String i : f) { for(int i1=0;i1<39;i1++) { Element pi = doc.createElement("Product_info"); rootElement.appendChild(pi); Element skn = doc.createElement("Skn"); skn.appendChild(doc.createTextNode(i)); pi.appendChild(skn); Element upc = doc.createElement("UPC"); upc.appendChild(doc.createTextNode("mook kim")); pi.appendChild(upc); Element pid = doc.createElement("Product-Id"); pid.appendChild(doc.createTextNode("mkyong")); pi.appendChild(pid); Element con = doc.createElement("Content"); con.appendChild(doc.createTextNode("100000")); pi.appendChild(con); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("C:\\Documents and Settings/janardhany/Desktop/testing1.xml")); transformer.transform(source, result); }}} System.out.println("Done"); }catch(ParserConfigurationException pce){ pce.printStackTrace(); }catch(TransformerException tfe){ tfe.printStackTrace(); } } }
Plz send me code..
Try this:
import java.io.*; import java.util.*; import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.WorkbookSettings; import jxl.read.biff.BiffException; class StoreCSVDataToXML{ public static void main(String[] args)throws Exception { ArrayList<String> list1=new ArrayList<String>(); ArrayList<String> list2=new ArrayList<String>(); ArrayList<String> list3=new ArrayList<String>(); WorkbookSettings ws = null; Workbook workbook = null; Sheet s = null; Cell rowData[] = null; int rowCount = 0; int columnCount = 0; int totalSheet = 0; ws = new WorkbookSettings(); ws.setLocale(new Locale("en", "EN")); workbook = Workbook.getWorkbook(new File("C:/data.csv"), ws); s = workbook.getSheet(0); rowCount = s.getRows(); columnCount = s.getColumns(); for(int i = 1; i < rowCount; i++){ rowData = s.getRow(i); if(rowData[0].getContents().length() != 0){ for(int j = 0; j < columnCount ;j++){ switch(j){ case 0: System.out.println("ID: "+rowData[j].getContents()); list1.add(rowData[j].getContents()); break; case 1: System.out.println("Name: "+rowData[j].getContents()); list2.add(rowData[j].getContents()); break; case 2: System.out.println("Address: "+rowData[j].getContents()); list3.add(rowData[j].getContents()); break; } } } } workbook.close(); DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder(); Document doc = documentBuilder.newDocument(); Element root = doc.createElement("Employee"); doc.appendChild(root); for(int i=0;i<list1.size();i++){ Element child = doc.createElement("Id"); root.appendChild(child); Text text = doc.createTextNode(list1.get(i).toString()); child.appendChild(text); Element child1 = doc.createElement("Name"); root.appendChild(child1); Text text1 = doc.createTextNode(list2.get(i).toString()); child1.appendChild(text1); Element child2 = doc.createElement("Address"); root.appendChild(child2); Text text2 = doc.createTextNode(list3.get(i).toString()); child2.appendChild(text2); } TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = sw.toString(); File file = new File("c:/new.xml"); FileWriter fw=new FileWriter(file,true); BufferedWriter bw = new BufferedWriter(fw); bw.write(xmlString); bw.flush(); bw.close(); System.out.println("Xml File is Created Successfully"); } }
For the above code, you need to download jxl.jar and put it into Java lib.
Ads