• 大小: 502KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: Java
  • 标签: csv  xml  java  

资源简介

java实现csv 转 xml,内含3种 方法

资源截图

代码片段和文件信息

package com.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opencsv.CSVReader;
import com.thoughtworks.xstream.XStream;
/**
 * 
 * use java bean to generate xml.
 * Need to call below classes:
 * TreeNode。java
 * TreeUtil。java
 *
 */
public class CsvToxml1 {
    public static void main(String args[]){
        System.out.println(“start...“);      
        String inputFile = “F:/temp/workspace/helloWord/data.csv“;
        String outFile = “F:/temp/workspace/helloWord/output/data1.xml“;
        try {
           BufferedReader reader = null;  
          reader = new BufferedReader(new FileReader(inputFile));
            String line = ““;
            List treeNodeList = new ArrayList();
            while((line=reader.readLine())!=null){ 
              String data[] = line.split(““);
              TreeNode treeNode = new TreeNode();  
              treeNode.setColumn1(data[0]);
              treeNode.setColumn2(data[1]);  
               //  System.out.println(data[0]+““+data[1]);
                 treeNodeList.add(treeNode);
            }
            
                 String xmlFile = new TreeUtil().parseNodeToxml(treeNodeList);            
                 FileWriter writer = new FileWriter(outFile);            
                 writer.write(xmlFile);
                 writer.flush();
                 writer.close();
            
            System.out.println(“completed...“);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
   
  
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        479  2017-02-09 23:54  helloWord\.classpath

     文件        385  2017-02-09 21:53  helloWord\.project

     文件        598  2017-02-09 21:53  helloWord\.settings\org.eclipse.jdt.core.prefs

     文件       2013  2017-02-10 07:32  helloWord\bin\com\test\CsvToxml1.class

     文件       1934  2017-02-10 00:31  helloWord\bin\com\test\CsvToxml2.class

     文件       2721  2017-02-10 07:33  helloWord\bin\com\test\CsvToxml3.class

     文件        910  2017-02-09 23:54  helloWord\bin\com\test\TreeNode.class

     文件       1415  2017-02-10 00:26  helloWord\bin\com\test\TreeUtil.class

     文件         47  2017-02-09 21:59  helloWord\data.csv

     文件      80720  2017-02-09 21:55  helloWord\lib\opencsv-3.9.jar

     文件       6073  2017-02-09 23:54  helloWord\lib\xmlpull_1_0_5.jar

     文件     492719  2017-02-09 21:55  helloWord\lib\xstream-1.4.jar

     文件        131  2017-02-10 07:33  helloWord\output\data1.xml

     文件        452  2017-02-10 07:33  helloWord\output\data2.xml

     文件        179  2017-02-10 07:33  helloWord\output\data3.xml

     文件       1948  2017-02-10 07:32  helloWord\src\com\test\CsvToxml1.java

     文件       1615  2017-02-10 00:31  helloWord\src\com\test\CsvToxml2.java

     文件       2464  2017-02-10 07:33  helloWord\src\com\test\CsvToxml3.java

     文件        482  2017-02-09 23:27  helloWord\src\com\test\TreeNode.java

     文件        793  2017-02-10 00:26  helloWord\src\com\test\TreeUtil.java

     目录          0  2017-02-10 00:27  helloWord\bin\com\test

     目录          0  2017-02-10 00:32  helloWord\src\com\test

     目录          0  2017-02-09 23:54  helloWord\bin\com

     目录          0  2017-02-09 21:54  helloWord\src\com

     目录          0  2017-02-09 21:53  helloWord\.settings

     目录          0  2017-02-09 23:54  helloWord\bin

     目录          0  2017-02-09 23:54  helloWord\lib

     目录          0  2017-02-10 00:32  helloWord\output

     目录          0  2017-02-09 21:54  helloWord\src

     目录          0  2017-02-10 00:31  helloWord

............此处省略3个文件信息

评论

共有 条评论