以学生对象为例
package com.situ.chapterwork; public class Student { private int id; private String name; private String sex; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
包含读取xml文件和写入xml文件
package com.situ.chapterwork; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Main { public static void main(String[] args) { List<Student> list = new ArrayList<>(); fecth(list);//读取xml文件信息 //打印看一下读取是否正确 for(Student s:list) { System.out.println(s); } stroe(list);//存储信息到xml文件 } private static void stroe(List<Student> list) {//这里指明Student对象,后面就可以直接用,不用把Student变成接口 // TODO Auto-generated method stub File file = new File("D:/Test/student.xml"); try { Document document = DocumentHelper.createDocument(); Element doc = document.addElement("class"); Element students = doc.addElement("students"); for(Student s :list) { Element student= students.addElement("student"); student.addAttribute("id", Integer.toString(s.getId())); Element name = student.addElement("name"); Element sex = student.addElement("sex"); Element age = student.addElement("age"); name.setText(s.getName()); sex.setText(s.getSex()); age.setText(Integer.toString(s.getAge())); } //写入xml OutputFormat format = OutputFormat.createPrettyPrint();//代码风格 format.setEncoding("UTF-8");//代码编码格式 XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);//新建文件流,写入xml文件 //写入一个文档 writer.write(document); writer.close(); } catch (FileNotFoundException e) { //TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { //TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { //TODO Auto-generated catch block e.printStackTrace(); } } private static void fecth(List<Student> list) {//这里指明Student对象,后面就可以直接用,不用把Student变成接口 // TODO Auto-generated method stub InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("com/situ/chapterwork/employee.xml"); SAXReader reader = new SAXReader(); try { //文档类 Document doc = reader.read(is); //返回根节点 Element root = doc.getRootElement(); Element students = root.element("students"); List<Element> studentList = students.elements(); for(Element e:studentList) {//遍历集合 Student stu1 = new Student(); stu1.setId(Integer.valueOf(e.attributeValue("id")).intValue());//Integer.parseInt(string)字符串转整形 stu1.setName(e.element("name").getText()); stu1.setSex(e.element("sex").getText()); stu1.setAge(Integer.valueOf(e.element("age").getText()));//Integer.parseInt(string)字符串转整形 list.add(stu1); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { is.close();//关闭流 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
<?xml version="1.0" encoding="UTF-8"?> <class> <students> <student id="1"> <name>王一</name> <sex>男</sex> <age>20</age> </student> <student id ="2"> <name>李二</name> <sex>男</sex> <age>21</age> </student> <student id="3"> <name>张三</name> <sex>女</sex> <age>20</age> </student> <student id="4"> <name>王五</name> <sex>男</sex> <age>22</age> </student> <students id = "5"> <name>马六</name> <sex>女</sex> <age>18</age> </students> </students> </class>
写入xml文件与序列化一致,只不过按照了xml文件格式读取写入