实体类
package com.sczy.entity;
public class Employee {
private int age;
private String empCode;
private String sex;
private String name;
public Employee(int age,String sex,String name,String empCode){
this.age = age;
this.sex = sex;
this.name = name;
this.empCode = empCode;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getEmpCode() {
return empCode;
}
public void setEmpCode(String empCode) {
this.empCode = empCode;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
工具类
package com.sczy.Tools;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
//获取类路径
public class Tloos {
//获取当前文件袋额类路径
public static String getPaths(){
String path = Tloos.class.getClassLoader().getResource("").getPath().toString();
try {
path = URLDecoder.decode(path,"utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return path;
}
public static Date GetDate(String data){
SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = sim.parse(data);
} catch (ParseException e) {
e.printStackTrace();
}
return date;
}
}
方法所在类
package com.sczy.entity;
import com.sczy.Tools.Tloos;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public class dom4jForXml {
private static DocumentFactory docufa = DocumentFactory.getInstance();
public static void toXMLForEmployee(List<Employee> empList,String fileName){
Document doc = docufa.createDocument();//创建一个Document实例
Element rootEle = docufa.createElement("employees");//设置根节点
doc.setRootElement(rootEle);
if(empList.size()!=0 && empList!=null){
for(Employee emp:empList){//遍历集合,将集合里的对象放进xml文件
Element empEle = docufa.createElement("employee");
Element codeEle = docufa.createElement("code");
codeEle.setText(emp.getEmpCode());
Element nameEle = docufa.createElement("name");
nameEle.setText(emp.getName());
Element ageEle = docufa.createElement("age");
ageEle.setText(emp.getAge()+"");
Element sexEle = docufa.createElement("sex");
sexEle.setText(emp.getSex());
//将一级子元素下的子元素加入到一级子元素
empEle.add(codeEle);
empEle.add(nameEle);
empEle.add(ageEle);
empEle.add(sexEle);
//将一级子元素放进跟元素
rootEle.add(empEle);
}
//将Dcument写入硬盘
String classPath = Tloos.getPaths();
//确定写入的路径
File xmlFile = new File(classPath+"com/sczy/doc/"+fileName);
XMLWriter write = null;
try {
write = new XMLWriter(new FileWriter(xmlFile), OutputFormat.createPrettyPrint());
write.write(doc);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
测试类
package com.sczy.test;
import com.sczy.entity.Employee;
import com.sczy.entity.dom4jForXml;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class testDom4jWriter {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<Employee> empList = new ArrayList<Employee>();
while(true){
System.out.println("请输入员工姓名");
String name = in.next();
System.out.println("请输入员工编号");
String code = in.next();
System.out.println("请输入员工年龄");
int age = in.nextInt();;
System.out.println("请输入员工性别");
String sex = in.next();
Employee emp = new Employee(age,sex,name,code);
empList.add(emp);
System.out.println("继续输入请输入1,不想输入请输入其他数字");
int tag = in.nextInt();
if(tag!=1)
break;
}
dom4jForXml.toXMLForEmployee(empList,"Employee.xml");//调用方法
}
}