一:项目分析
学生管理系统:
通过利用xml文件的存储功能来存储学生信息。再用dom4j解析器来对xml文件进行操作,也会用到一点xpath的东西。
二:实现步骤
1.Dom4j工具类
用来简化对xml的操作:
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
* Dom4jUtils小工具类,通过这个小工具类可以更方便用Dom4j技术对xml的操作
*/
public class Dom4jUtils {
public static final String PATH="src/Student.xml";
//通过这个方法得到document对象
public static Document getDocument(String path) throws DocumentException {
SAXReader saxReader=new SAXReader();
Document document=saxReader.read(path);
return document;
}
//通过这个方法回写xml文件
public static void xmlWriters(String path,Document document) throws IOException {
//创建格式化输出流
OutputFormat format=OutputFormat.createPrettyPrint();
XMLWriter xmlWriter=new XMLWriter(new FileOutputStream(path),format);
xmlWriter.write(document);
xmlWriter.close();
}
}
2.创建xml文件:
用来存储学生的信息(充当存储器的功能):
<?xml version="1.0" encoding="UTF-8"?>
<student>
<s1>
<id>2</id>
<name>张三</name>
<age>15</age>
</s1>
<s2>
<id>3</id>
<name>李四</name>
<age>45</age>
</s2>
</student>
student类:方便存储
/*
* 创建学生对象,实现各种操作时通过学生对象的方法来实现
*/
public class Student {
private String id;
private String name;
private String age;
public Student(String id, String name, String age) {
this.id = id;
this.name = name;
this.age = age;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
3.Service层
实现对文件的操作:
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import cn.xyz.Dom4jUtils.Dom4jUtils;
import cn.xyz.io.Student;
/**
* 操作层用来操作学生对象
* @author lenovo
*
*/
public class StudentService {
//添加学生
public static void addStudent(Student student) throws Exception {
/*
* 1.得到document对象
* 2.找到根节点
* 3.在根节点下添加学生节点
* 4.设置各种参数的值
* 5.回写xml文件
*/
Document document=Dom4jUtils.getDocument(Dom4jUtils.PATH);
Element root=document.getRootElement();
Element s3=root.addElement("s3");
Element id=s3.addElement("id");
id.setText(student.getId());
Element name=s3.addElement("name");
name.setText(student.getName());
Element age=s3.addElement("age");
age.setText(student.getAge());
//回写xml文件
Dom4jUtils.xmlWriters(Dom4jUtils.PATH, document);
}
//删除学生,根据学生的id来删除
public static void delStudent(String id) throws Exception {
/*
* 1.得到document
* 2.遍历所有的id
* 3.判断id的值是否和参数一样,如果一样,就删除这个学生
* 4.回写xml文件
*/
Document document=Dom4jUtils.getDocument(Dom4jUtils.PATH);
List<Node>list=document.selectNodes("//id");
for(int i=0;i<list.size();i++) {
Node node=list.get(i);
System.out.println(node.getText().equals(id));
if(node.getText().equals(id)) {
Element student=node.getParent();
Element root=student.getParent();
root.remove(student);
}
}
Dom4jUtils.xmlWriters(Dom4jUtils.PATH, document);
}
//查询学生,根据学生的id来查询
public static void selectStudent(String id) throws Exception {
/*
* 1.得到document
* 2.得到所有id
* 3.比较id的值
* 4.如果一样,返回这个节点的所有信息
*/
Document document=Dom4jUtils.getDocument(Dom4jUtils.PATH);
List<Node> list=document.selectNodes("//id");
for(int i=0;i<list.size();i++) {
Node node=list.get(i);
if(node.getText().equals(id)) {
Element parent=node.getParent();
Element d=parent.element("id");
Element name=parent.element("name");
Element age=parent.element("age");
System.out.println("学生的id是:"+d.getText()+"姓名是:"+name.getText()+"年龄是:"+age.getText());
}
}
}
}
4.测试方法
import cn.xyz.io.Student;
import cn.xyz.service.StudentService;
public class TextStudent {
public static void main(String[] args) {
try {
//添加学生
// Student s=new Student ("23","王五","13");
// StudentService.addStudent(s);
//删除学生
//StudentService.delStudent("23");
//查询学生
StudentService.selectStudent("2");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}