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;
}
}