注册CSDN快六个月了,之前一直是看园子里的文章,或者碰到问题时,进来查点资料,今天终于决定写一篇自己在园子里的blog。
好吧,鉴于水平太菜,就只记录过程,其中原理啥的并不是很明晰,所以此blog只在记录,莫BS哈。
神马是CSV文件呢,看起来和excel文件很像,具体有神马不同,见这位圆友的blog哈,/luweifeng1983/article/details/3582657。简单来说,本质是文本文件,但文件分多行,内容以“,”分割,系统默认是excel打开的一种文件。
好吧,我们以下面这个通讯录CSV文件为例。
下面开始转换过程的讲解喽。
1. 读取csv文件
1)Java操作csv文件有专门的的API叫javacsv.jar
下载地址如下:
/project/showfiles.php?group_id=33066
2)自己项目中使用的工具是dataimport_3.3.0-rc1.jar
下载地址如下:
/snapshot//maven2/net.sf.squirrel-sql.plugins/dataimport/3.3.0-rc1
读取示例代码:
//read csv file
CsvReader cr = new CsvReader(new InputStreamReader(new FileInputStream(new File(filename)),
"UTF-8"));
//read csv file headers
cr.readHeaders();
//read csvv file records
cr.readRecord();
2.将读取到的csv文件转换成xml格式
1)先确定自己要求替换完的xml数据的格式,假如如下图定义,可设置为资源文件,命名为addresslist.xml.
<?xml version="1.0" encoding="UTF-8"?>
<AddressList>
<Person>
<Name>@name@</Name>
<Company>@company@</Company>
<MobilePhone>@mobilephone@</MobilePhone>
<Address>@address@</Address>
<Relationship>@relationship@</Relationship>
</Person>
</AddressList>
2)读取资源文件addresslist.xml。
String mapping = "addresslist.xml";
URL url = ConfigurationUtils.locate(mapping);
SAXReader reader = new SAXReader();
Document doc = reader.read(url);
3)完成替换(此步骤是将CSV文件中的数据记录按行替换资源文件中的占位符,生成xml消息)
//find Node "Name" in addresslist.xml
Element name = (Element) doc.selectSingleNode("/AddressList/Person/Name");
//read "name" from csv file and set value for Node "Name"
name.setText(cr.get("name"));
//find Node "Company" in addresslist.xml
Element company = (Element) doc.selectSingleNode("/AddressList/Person/Company");
//read "company" from csv file and set value for Node "Company"
company.setText(cr.get("company");
//find Node "MobilePhone" in addresslist.xml
Element mobilephone = (Element) doc.selectSingleNode("/AddressList/Person/MobilePhone");
//read "mobile-phone" from csv file and set value for Node "MobilePhone"
mobilephone.setText(cr.get("mobile-phone"));
//find Node "Address" in addresslist.xml
Element address = (Element) doc.selectSingleNode("/AddressList/Person/Address");
//read "address" from csv file and set value for Node "Address"
mobilephone.setText(cr.get("address"));
//find Node "RelationShip" in addresslist.xml
Element relationship = (Element) doc.selectSingleNode("/AddressList/Person/RelationShip");
//read "relationship" from csv file and set value for Node "RelationShip"
relationship.setText(cr.get("relationship"));
4)将xml消息转换成文本输出
String record= doc.asXML();
可配合while,循环转换csv文件中的三条记录。
完整的java code来了呢
1 package demo.csv; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.io.UnsupportedEncodingException; 9 import .URL;10 import java.util.ArrayList;11 import java.util.Iterator;12 import java.util.List;13 14 import mons.configuration.ConfigurationUtils;15 import org.dom4j.Document;16 import org.dom4j.Element;17 import org.dom4j.io.SAXReader;18 19 import com.csvreader.CsvReader;20 21 public class CsvDemo {2223private static List<String> transformCsv2Xml(String filename) {24 25 String mapping = "src/demo/csv/address.xml";26 27 List<String> messages = new ArrayList<String>();28 29 try {30 URL url = ConfigurationUtils.locate(mapping);31 SAXReader reader = new SAXReader();32 Document doc = reader.read(url);33 34 //read csv file35 CsvReader cr = new CsvReader(new InputStreamReader(new FileInputStream(new File(filename)), 36 "UTF-8"));37 //read csv file headers 38 cr.readHeaders();39 40 //read csv file records41 //cr.readRecord();42 while (cr.readRecord()) {43 //find Node "Name" in addresslist.xml44 Element name = (Element) doc.selectSingleNode("/AddressList/Person/Name");45 //read "name" from csv file and set value for Node "Name"46 name.setText(cr.get("name").trim());47 //find Node "Company" in addresslist.xml48 Element company = (Element) doc.selectSingleNode("/AddressList/Person/Company");49 //read "company" from csv file and set value for Node "Company"50 company.setText(cr.get("company").trim());51 //find Node "MobilePhone" in addresslist.xml52 Element mobilephone = (Element) doc.selectSingleNode("/AddressList/Person/MobilePhone");53 //read "mobile-phone" from csv file and set value for Node "MobilePhone"54 mobilephone.setText(cr.get("mobile-phone").trim());55 //find Node "Address" in addresslist.xml56 Element address = (Element) doc.selectSingleNode("/AddressList/Person/Address");57 //read "address" from csv file and set value for Node "Address"58 address.setText(cr.get("address").trim());59 //find Node "RelationShip" in addresslist.xml60 Element relationship = (Element) doc.selectSingleNode("/AddressList/Person/Relationship");61 //read "relationship" from csv file and set value for Node "RelationShip"62 relationship.setText(cr.get("relationship").trim());63 // break;64 messages.add(doc.asXML());65 }66 67 } catch (UnsupportedEncodingException e) {68 69 } catch (FileNotFoundException e) {70 71 } catch (IOException e) {72 73 } catch (Exception ex) {74 75 }76 return messages;77}78 79 80public static void main(String[] args) {81 82 List<String> addresslist = transformCsv2Xml("src/demo/csv/data.csv");83 Iterator<String> it = addresslist.iterator();84 while(it.hasNext()){85 System.out.println(it.next());86 }87 88}89 90 }
View Code
介绍完毕,,,虽然内容很少,但完整记录下来好费时间,可能是太菜,思路也不太清楚的原因吧,好吧,还是累屎姐了,不知道下一次下决心写会是神马时候。
-05-14
如果觉得《java转换CSV文件生成xml格式数据》对你有帮助,请点赞、收藏,并留下你的观点哦!