需求:
考试系统需求文档
1、 以如的exam.xml文件为例
package zgq.UI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import zgq.beans.Student;
import zgq.dao.StudentDao;
import zgq.exception.StudentNotExistException;
public class Main {
public static void main(String[] args) {
try {
System.out.println("添加用户:(a) 删除用户:(b) 查询成绩(c");
System.out.print("请输入操作类型:");
// 获取用户输入
BufferedReader bufr = new BufferedReader(new InputStreamReader(
System.in));
String in = bufr.readLine();
if ("a".equals(in)) {
System.out.print("请输入学生姓名:");
String name = bufr.readLine();
System.out.print("请输入学生准考证号:");
String examid = bufr.readLine();
System.out.print("请输入学生身份证号:");
String idcard = bufr.readLine();
System.out.print("请输入学生所在地:");
String location = bufr.readLine();
System.out.print("请输入学生成绩:");
String grade = bufr.readLine();
Student s = new Student();
s.setExamid(examid);
s.setGrade(Double.parseDouble(grade));
s.setIdcard(idcard);
s.setLocation(location);
s.setName(name);
StudentDao dao = new StudentDao();
dao.add(s);
System.out.println("添加成功!!");
}
else if ("b".equals(in)) {
System.out.print("请输入要删除的学生的姓名:");
String name = bufr.readLine();
try{
StudentDao dao = new StudentDao();
dao.delete(name);
System.out.println("删除成功!!");
}catch (StudentNotExistException e) {
System.out.println("您要删除的用户不存在!!");
}
}
else if ("c".equals(in)) {
System.out.print("请输入要删除的学生的准考证号:");
String examid = bufr.readLine();
StudentDao studentDao=new StudentDao();
Student student= studentDao.find(examid);
if(student!=null){
System.out.println("Examid:"+student.getExamid());
System.out.println("Idcard:"+student.getIdcard());
System.out.println("Name:"+student.getName());
System.out.println("Location:"+student.getLocation());
System.out.println("Grade:"+student.getGrade());
}
else {
System.out.println("您输入的准考证号有误,未能查到相关信息");
}
}
else {
System.out.println("您输入的有误!运行截止。");
return;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("对不起,出错了!!");
}
}
}
utils包
package zgq.utils;
import java.io.FileOutputStream;
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 {
//在工具类中,尽量将字段和方法定义为静态的
private static String filename="src/exam.xml";
//获取Document文档
public static Document getDocument() throws Exception{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder= factory.newDocumentBuilder();
Document document=builder.parse(filename);
return document;
}
//写入Document内容
public static void writeToXml(Document document) throws Exception{
TransformerFactory factory=TransformerFactory.newInstance();
Transformer former= factory.newTransformer();
former.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}
}
bean包
package zgq.beans;
public class Student {
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;
}
}
DAO包
package zgq.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import zgq.beans.Student;
import zgq.exception.StudentNotExistException;
import zgq.utils.XmlUtils;
public class StudentDao {
//添加信息
public void add(Student s){
try {
//获取exam.xml文档dom树
Document document=XmlUtils.getDocument();
//创建出封装学生信息的标签<student examid="222" idcard="111"></student>
Element student_tag=document.createElement("student");
student_tag.setAttribute("examid",s.getExamid());
student_tag.setAttribute("idcard", s.getIdcard());
//创建用于封装学生姓名的标签
Element student_name=document.createElement("name");
student_name.setTextContent(s.getName());
//创建用于封装学生所在地的标签
Element student_location=document.createElement("location");
student_location.setTextContent(s.getLocation());
//创建用于封装学生成绩的标签
Element student_grade=document.createElement("grade");
student_grade.setTextContent(s.getGrade()+"");
//将姓名所在地和成绩挂载到student_tag标签上
student_tag.appendChild(student_name);
student_tag.appendChild(student_location);
student_tag.appendChild(student_grade);
//将新得到的文件内容挂载到exam节点上
document.getElementsByTagName("exam").item(0).appendChild(student_tag);
//更新xml文件
XmlUtils.writeToXml(document);
} catch (Exception e) {
throw new RuntimeException(e); //unchecked exception(运行时异常)
//checked exception(编译时异常)
}
}
//查找信息
public Student find(String examid){
try {
Document document=XmlUtils.getDocument();
//获取student标签集合
NodeList students=document.getElementsByTagName("student");
//循环获取 查看student的属性examid
for(int i=0;i<students.getLength();i++){
Element student_tag=(Element) students.item(i);
if(student_tag.getAttribute("examid").equals(examid)){ //找到匹配学生
Student s=new Student();
s.setExamid(examid);
s.setIdcard(student_tag.getAttribute("idcard"));
s.setName(student_tag.getElementsByTagName("name").item(0).getTextContent());
s.setLocation(student_tag.getElementsByTagName("location").item(0).getTextContent());
s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
return s;
}
}
return null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void delete(String name) throws StudentNotExistException{ //根据姓名进行删除操作
Document document;
try {
document = XmlUtils.getDocument();
NodeList students=document.getElementsByTagName("name"); //通过name获取学生集合
for(int i=0;i<students.getLength();i++){
if(students.item(i).getTextContent().equals(name)){
//得到指定节点 进行删除操作
students.item(i).getParentNode().getParentNode().removeChild(students.item(i).getParentNode());
//重新写入xml文件
XmlUtils.writeToXml(document);
return;
}
}
throw new StudentNotExistException("删除节点不存在");
}catch(StudentNotExistException e){
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
异常包
package zgq.exception;
public class StudentNotExistException extends Exception {
//自定义类
public StudentNotExistException() {
// TODO 自动生成的构造函数存根
}
public StudentNotExistException(String message) {
super(message);
// TODO 自动生成的构造函数存根
}
public StudentNotExistException(Throwable cause) {
super(cause);
// TODO 自动生成的构造函数存根
}
public StudentNotExistException(String message, Throwable cause) {
super(message, cause);
// TODO 自动生成的构造函数存根
}
// public StudentNotExistException(String message, Throwable cause,
// boolean enableSuppression, boolean writableStackTrace) {
// super(message, cause, enableSuppression, writableStackTrace);
// // TODO 自动生成的构造函数存根
// }
}
测试包:
package junit.test;
import org.junit.Test;
import zgq.beans.Student;
import zgq.dao.StudentDao;
import zgq.exception.StudentNotExistException;
public class StudentDaoTest {
@Test
public void testAdd(){
StudentDao studentDao=new StudentDao();
Student student=new Student();
student.setExamid("123");
student.setIdcard("321");
student.setName("王五");
student.setLocation("北京");
student.setGrade(97);
studentDao.add(student);
}
@Test
public void testFind(){
StudentDao studentDao=new StudentDao();
Student student= studentDao.find("123");
System.out.println("Examid:"+student.getExamid());
System.out.println("Idcard:"+student.getIdcard());
System.out.println("Name:"+student.getName());
System.out.println("Location:"+student.getLocation());
System.out.println("Grade:"+student.getGrade());
}
@Test
public void testDelete() throws StudentNotExistException {
StudentDao studentDao=new StudentDao();
studentDao.delete("王五");
}
}
项目地址:https://github.com/Mrzhoug/exam_base_xml.git