資源簡介
這是一個基于XML操作的學生信息管理小系統,實現了對學生信息的錄入,查找,刪除等操作。可以說是一個簡易的“數據庫”綜合小應用!
代碼片段和文件信息
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? 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 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?studen
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????301??2016-01-20?15:39??StudentSystem\.classpath
?????文件????????389??2016-01-20?15:39??StudentSystem\.project
?????文件????????598??2016-01-20?15:39??StudentSystem\.settings\org.eclipse.jdt.core.prefs
?????文件???????4343??2016-01-20?19:52??StudentSystem\bin\dao\StudentDao.class
?????文件???????1282??2016-01-20?15:47??StudentSystem\bin\domain\Student.class
?????文件????????604??2016-01-20?20:04??StudentSystem\bin\Student.xm
?????文件???????1806??2016-01-20?16:32??StudentSystem\bin\utils\xm
?????文件???????3173??2016-01-20?17:51??StudentSystem\bin\view\Main.class
?????文件???????4174??2016-01-20?19:52??StudentSystem\src\dao\StudentDao.java
?????文件????????776??2016-01-20?15:47??StudentSystem\src\domain\Student.java
?????文件????????604??2016-01-20?20:04??StudentSystem\src\Student.xm
?????文件???????1130??2016-01-20?16:32??StudentSystem\src\utils\xm
?????文件???????2496??2016-01-20?17:51??StudentSystem\src\view\Main.java
?????目錄??????????0??2016-01-20?15:47??StudentSystem\bin\dao
?????目錄??????????0??2016-01-20?15:41??StudentSystem\bin\domain
?????目錄??????????0??2016-01-20?15:51??StudentSystem\bin\utils
?????目錄??????????0??2016-01-20?16:12??StudentSystem\bin\view
?????目錄??????????0??2016-01-20?15:47??StudentSystem\src\dao
?????目錄??????????0??2016-01-20?15:41??StudentSystem\src\domain
?????目錄??????????0??2016-01-20?15:51??StudentSystem\src\utils
?????目錄??????????0??2016-01-20?16:12??StudentSystem\src\view
?????目錄??????????0??2016-01-20?15:39??StudentSystem\.settings
?????目錄??????????0??2016-01-20?20:05??StudentSystem\bin
?????目錄??????????0??2016-01-20?18:05??StudentSystem\src
?????目錄??????????0??2016-01-20?15:39??StudentSystem
-----------?---------??----------?-----??----
????????????????21676????????????????????25
評論
共有 條評論