java test

Ads

 
 

Share on Google+Share on Google+

Janardhan yadav
java test
2 Answer(s)      6 years and 3 months ago
Posted in : XML

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..

Ads
View Answers

June 17, 2011 at 3:29 PM


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");
  }
}


June 17, 2011 at 3:32 PM


For the above code, you need to download jxl.jar and put it into Java lib.


Ads









Related Tutorials/Questions & Answers:
java test
java test  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...:\\Documents and Settings/janardhany/Desktop/test_sample.txt
how to conduct the test in java
how to conduct the test in java  how to conduct the test in java
Advertisements
java test - Java Beginners
java test   Trace through the following program segment and state clearly the output that will be generated: public static void main(String [] args){ int no1; int no2; for (no1 = -2; no1 <= 1; no1++){ for (no2
Create test engine - Java Beginners
Create test engine  How can create a test engine dat generates questions randomly using javax.swing in java
online test application in java
Online Test Application Table Of Contents Introduction To Application Database Design Creating Data Access Object (DAO) Design Pattern Creating Views Writing Actions Mapping Application Admin Module User Module Download
online test application in java
online test application in java  Can questions come in html file (test.html) if we link test.jsp with test.html   We have taken radiobuttons in our application, you can take textarea, textfield etc. Here we have created
Java File Handling Test Results
Java File Handling Test Results   Is somebody able to help me with this Write a program to read each name from the file and prompt the user... java.io.*; public class Test { public static void main(String[] args) throws
junit test case - Java Beginners
junit test case  how to use junit for testing email id format  Hi Friend, Please visit the following link: http://www.roseindia.net/junit/ Hope that it will be helpful for you. Thanks
Version of com.amazonaws>aws-java-sdk-test-utils dependency
List of Version of com.amazonaws>aws-java-sdk-test-utils dependency
code for timer in java netbeans for online test series countdown timer
code for timer in java netbeans for online test series countdown timer  code for timer in java netbeans for online test series countdown timer
How to bookmark and preview last question in a java test engine - Java Beginners
How to bookmark and preview last question in a java test engine  Am creating a test engine with java, how can i generate questions randomly, allow a user bookmark a question view previous question and review questions
How to bookmark and preview last question in a java test engine - Java Beginners
How to bookmark and preview last question in a java test engine  Am creating a test engine with java, how can i generate questions randomly, allow a user bookmark a question view previous question and review questions
online test project on java using servlets and jsp
online test project on java using servlets and jsp  as i am doing online test project on java using jsp and servlets ,,,the problem is in the code...;Here is a jsp application. Here we have created a table test
test case
test case  Hi Can you explain me how to write test case in java. regards kennedy
Maven Dependency aws-java-sdk-test-utils >> 1.10.10
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.10 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.11
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.11 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.12
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.12 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.13
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.13 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.14
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.14 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.15
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.15 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.16
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.16 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.17
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.17 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.18
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.18 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.19
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.19 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.20
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.20 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.6
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.6 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.7
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.7 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.8
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.8 in your project
Maven Dependency aws-java-sdk-test-utils >> 1.10.9
You should include the dependency code given in this page to add Maven Dependency of com.amazonaws >> aws-java-sdk-test-utils version1.10.9 in your project
putting words to line number form a java file/test file and show occurrence
putting words to line number form a java file/test file and show occurrence  hi all i want putting words to line number form a java file and show... and BinarySearchTree and please help me in java coding
WRITE AND TEST
WRITE AND TEST   WRITE AND TEST PROGRAM TO ACCEPT A STRING? 1).WHICH STARTS WITH 'S' OR 's' 2).ENDS WITH "nd" 3) THE LENGTH OF THE STRING SHOULD BE 10 OR LESS