为了更好地应用XML,就写了这个小项目。
下面是我的项目的目录结构
项目思路
- dao是Date Access Object 数据访问层,主要是负责操作数据
- domain是实体层,类似于bean层,放置项目用到的实体Student
- utils层是有关于XML操作的部分(一般实际开发中是数据库操作部分)
- view层是视图层(实际开发中是GUI层,与用户直接打交道)
- Student.xml在这里相当于我们的一个小小的数据库
dao层设计
按照习惯的命名规则,我命名为StudentDao.java,具体实现的功能有添加学生信息,查找学生信息,删除学生信息。这里仅仅是直接对数据操作的模块,而把底层的操作XML文档的放到了utils中。这也在一定程度上实现了分层的思想,虽然这并不明显,也并不必需!
package dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import utils.XMLUtils;
import domain.Student;
public class StudentDao {
/**
* 添加学生信息模块
* @param student
*/
public void add(Student student) {
try {
Document document = XMLUtils.getDocument();
Element student_node = document.createElement("student");
student_node.setAttribute("examid", student.getExamid());
student_node.setAttribute("idcard", student.getIdcard());
Element name = document.createElement("name");
name.setTextContent(student.getName());
Element location = document.createElement("location");
location.setTextContent(student.getLocation());
Element grade = document.createElement("grade");
// 这里是一个类型转换的隐藏之处。不太明显但是却十分的重要
grade.setTextContent(student.getGrade() + "");
// 将新生成的三个子节点插入到student标签内
student_node.appendChild(name);
student_node.appendChild(location);
student_node.appendChild(grade);
// 对总的xml文档中添加一个学生信息
document.getElementsByTagName("exam").item(0)
.appendChild(student_node);
//将内存中的操作对象写回到xml文件,真正实现对文件的操作
XMLUtils.write2Xml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);
}
}
public void delete(String name) {
try {
Document document = XMLUtils.getDocument();
NodeList name_node_list = document.getElementsByTagName("name");
for (int i = 0; i < name_node_list.getLength(); i++) {
if (name_node_list.item(i).getTextContent().equals(name)) {
Element person_node = (Element) name_node_list.item(i)
.getParentNode();
Element exam_node = (Element) person_node.getParentNode();
exam_node.removeChild(person_node);
//不要忘记将操作过的数据写回,否则原信息是不会发生变化的
XMLUtils.write2Xml(document);
System.out.println("恭喜,学生信息删除成功!");
}
}
} catch (Exception e) {
System.out.println("对不起,删除操作未成功完成!请重试!");
throw new RuntimeException(e);
}
}
/**
* 给定学生的考号查找该同学的详细的信息(不用姓名的原因是姓名具有不唯一性)
* @param examid
* @return
*/
public Student find(String examid) {
Student student=null;
try {
Document document = XMLUtils.getDocument();
NodeList examid_node_list = document.getElementsByTagName("student");
//查找准考证号与查找值相一致的学生节点
for(int i=0; i<examid_node_list.getLength();i++){
Element examid_element = (Element) examid_node_list.item(i);
if(examid_element.getAttribute("examid").equals(examid.toString().trim())){
//采用非递归的方式获取student的详细信息
student = getStudentInfo(examid_element);
return student;
}else{
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("对不起,未能正确的找到您要查找的学生的姓名!请确认后重新尝试!");
}
return student;
}
/**
* 给定一个节点,采用非递归的方式遍历该学生节点的详细的信息
* 缺点:不能很好地复用代码,代码维护性较差
*/
public Student getStudentInfo(Element node){
Student student = new Student();
if(node!=null){
String examid = node.getAttribute("examid");
String idcard = node.getAttribute("idcard");
NodeList node_list = node.getChildNodes();
//由于collection 的不确定性,是随机取出的数据,导致bean中的数据不太对应
Node node_name = node_list.item(1);
String name = node_name.getTextContent();
Node node_location = node_list.item(2);
String location = node_location.getTextContent();
Node node_grade = node_list.item(0);
String grade = node_grade.getTextContent()+"0.0";
//将获取的信息保存到bean中,并作为返回值返回!
student.setExamid(examid);
student.setGrade(Double.parseDouble(grade));
student.setIdcard(idcard);
student.setLocation(location);
student.setName(name);
return student;
}
System.out.println("this find operation is false!");
return null;
}
}
domain--实体层(bean)
package domain;
public class Student {
//注意将Student具有的属性设置为私有的成员,然后设置getter,setter来访问
private String idcard;
private String examid;
private String name;
private String location;
private double grade;
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
Utils层充当工具,关联底层操作
package utils;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
public class XMLUtils {
//all the utils methods are static
public static Document getDocument() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("src/Student.xml"));
return document;
}
public static void write2Xml(Document document) throws Exception{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer=factory.newTransformer();
//wrapper the two arguments
DOMSource xmlSource = new DOMSource(document);
StreamResult targetResult = new StreamResult(new File("src/Student.xml"));
transformer.transform(xmlSource, targetResult);
}
}
小感悟:一般来说工具类的方法会做成静态的私有的,这样可以免去创建对象,又能很好的使用到它!
view层(与用户直接交互的模块)
这里仅仅是简单的一些有好的交互,并没有什么复杂的地方
package view;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import dao.StudentDao;
import domain.Student;
public class Main {
public static void main(String []args) throws Exception{
System.out.println("添加学生(a),查找学生(f),删除学生(d)");
System.out.println("请输入操作类型:");
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String type = reader.readLine();
if(type.equalsIgnoreCase("a")){
//add student
//name examid idcard location grade
try{
System.out.println("请输入学生姓名: ");
String name = reader.readLine();
System.out.println("请输入学生准考证号: ");
String examid = reader.readLine();
System.out.println("请输入学生身份证号: ");
String idcard = reader.readLine();
System.out.println("请输入学生所在地: ");
String location = reader.readLine();
System.out.println("请输入学生成绩: ");
String grade = reader.readLine();
Student student = new Student();
student.setExamid(examid);
student.setIdcard(idcard);
student.setLocation(location);
student.setName(name);
student.setGrade(Double.parseDouble(grade));
StudentDao studentDao = new StudentDao();
studentDao.add(student);
System.out.println("恭喜:学生信息添加成功!");
}catch(Exception e){
e.printStackTrace();
System.out.println("对不起,学生信息数据添加失败!");
}
}else if(type.equalsIgnoreCase("f")){
//find student
System.out.println("请输入学生的准考证号");
StudentDao studentDao = new StudentDao();
String examid = reader.readLine();
Student student = studentDao.find(examid);
PrintStudentInfo(student);
}else if(type.equalsIgnoreCase("d")){
//delete student
System.out.println("请输入学生的姓名");
StudentDao studentDao = new StudentDao();
String name = reader.readLine();
studentDao.delete(name);
}else{
System.out.println("Not Support!");
}
}
private static void PrintStudentInfo(Student student) {
// TODO Auto-generated method stub
if(student!=null){
System.out.println("学生考号: \t"+student.getExamid());
System.out.println("学生姓名: \t"+student.getName());
System.out.println("学生身份证号码 : \t"+student.getIdcard());
System.out.println("学生所在地: \t"+student.getLocation());
System.out.println("学生成绩: \t"+student.getGrade());
}
}
}
处理素材和结果
Student.xml文件示意如下:
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>Spring</name>
<location>DaLian</location>
<grade>89</grade>
</student>
<student examid="444" idcard="333">
<name>Summer</name>
<location>ShangHai</location>
<grade>97</grade>
</student>
<student examid="201492115" idcard="410728">
<name>郭璞</name>
<location>大连</location>
<grade>89.0</grade>
</student>
<student examid="123" idcard="1234567">
<name>未命名</name>
<location>不知道</location>
<grade>0.0</grade>
</student>
</exam>
操作结果:
添加学生操作
添加学生信息结果
删除学生操作
删除学生结果
查找学生详细信息
总结:
优点:
使用了分层思想(虽然有些地方并不是很明显)
模块化的操作时的代码逻辑更加的清晰
较好的实现了对xml文件的CRUD操作
结合了具体的项目,运用到了相关的知识点
缺点:代码处理上仍旧有很大的重复性
设计模式的运用不是很明显
界面有点糟糕
有待改进之处:
- 使用GUI,rich用户体验
- 底层处理逻辑应更加的精简