一:項目分析
學生管理系統:
通過利用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();
}
}
}