一、需求
从csv文件读取内容,写入到xml文件中去
二、实现
第一步先从csv读取内容
第二步把读取的内容写到xml
注意:读取csv文件的内容是利用的hutool框架,所以要在pom文件加入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.1.19</version>
</dependency>
代码如下:
package com.lsl.mylsl.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.csv.CsvData;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvRow;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.CharsetUtil;
import com.lsl.mylsl.module.Student;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class MyTest {
public static void main(String[] args) {
List<Student> studentList = readCsv();
createXml(studentList);
}
/**
* 利用hutool框架读取csv文件内容到list
*/
public static List<Student> readCsv() {
CsvReader reader = CsvUtil.getReader();
//从文件中读取CSV数据
CsvData data = reader.read(FileUtil.file("C:\\Users\\perfe\\Desktop\\list.csv"), CharsetUtil.CHARSET_GBK);
List<CsvRow> rows = data.getRows();
for (CsvRow row : rows) {
List<String> rawList = row.getRawList();
System.out.println(rawList);
}
//csv文件的列依次是id,stuName,age,addr
List<Student> studentList = new ArrayList<>();
for (int i = 1; i < rows.size(); i++) {
CsvRow row = rows.get(i);
Student stu = new Student();
List<String> rawList = row.getRawList();
stu.setId(Integer.valueOf(rawList.get(0)));
stu.setStuName(rawList.get(1));
stu.setAge(Integer.valueOf(rawList.get(2)));
stu.setAddr(rawList.get(3));
studentList.add(stu);
}
System.out.println(studentList);
return studentList;
}
/**
* 创建xml并写入从csv读取的内容
* @param studentList
*/
public static void createXml(List<Student> studentList){
try {
// 创建解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document document = db.newDocument();
// 不显示standalone="no"
document.setXmlStandalone(true);
Element students = document.createElement("students");
for (Student stu : studentList){
Element student = document.createElement("student");
student.setAttribute("flag", "true");
Element id = document.createElement("id");
id.setTextContent(stu.getId()+"");
student.appendChild(id);
Element name = document.createElement("name");
name.setTextContent(stu.getStuName());
student.appendChild(name);
Element age = document.createElement("age");
age.setTextContent(stu.getAge()+"");
student.appendChild(age);
Element addr = document.createElement("addr");
addr.setTextContent(stu.getAddr());
student.appendChild(addr);
students.appendChild(student);
}
// 将students节点(已包含student)添加到dom树中
document.appendChild(students);
// 创建TransformerFactory对象
TransformerFactory tff = TransformerFactory.newInstance();
// 创建 Transformer对象
Transformer tf = tff.newTransformer();
// 输出内容是否使用换行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
// 创建xml文件并写入内容
tf.transform(new DOMSource(document), new StreamResult(new File("D:\\Download\\students.xml")));
System.out.println("生成student.xml成功");
} catch (Exception e) {
e.printStackTrace();
System.out.println("生成student.xml失败");
}
}
// public static void filter(List<String> list){
//
// for (int m=list.size()-1;m==0;m--){
// if (!"a".equals(list.get(m))){
// list.remove(m);
// }
// }
//
// }
//
// public byte[] blobToByte(Blob blob) throws Exception {
// byte[] bytes = null;
// try {
// InputStream in=blob.getBinaryStream();
// BufferedInputStream inBuffered = new BufferedInputStream(in);
// ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
// byte[] temp = new byte[1024];
// int size = 0;
// while ((size = inBuffered.read(temp)) != -1) {
// out.write(temp, 0, size);
// }
// inBuffered.close();
// in.close();
// bytes = out.toByteArray();
// } catch(Exception ex){
// ex.printStackTrace();
// }
// return bytes;
// }
}
生成截图如下: