功能实现要求:
1、任务页面
2、实现学生信息添加
3、实现学生信息查询
4、实现学生的删除功能
项目目录结构:
exam.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student examid="444" idcard="333">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
Student.java
package domain;
public class Student {
private static String idcard;
private static String examid;
private static String name;
private static String location;
private static double grade;
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
Student.idcard = idcard;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
Student.examid = examid;
}
public String getName() {
return name;
}
public void setName(String name) {
Student.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
Student.location = location;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
Student.grade = grade;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
StudentDao.java
package dao;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import domain.Student;
import utils.XMLUtil;
public class StudentDao {
public void add(Student student) throws SAXException, IOException, ParserConfigurationException, TransformerException {
Document document = XMLUtil.getDocument();
Student s = new Student();
Element nodeStut = (Element) document.createElement("student");
Node examNode = document.getElementsByTagName("exam").item(0);
examNode.appendChild(nodeStut);
Node nodeName = document.createElement("name");
nodeName.setTextContent(s.getName());
nodeStut.appendChild(nodeName);
nodeStut.setAttribute("examid", s.getExamid());
nodeStut.setAttribute("idcard", s.getIdcard());
Node nodeLocation = document.createElement("location");
nodeLocation.setTextContent(s.getLocation());
nodeStut.appendChild(nodeLocation);
Node nodeGrade = document.createElement("grade");
nodeGrade.setTextContent(s.getGrade()+"");
nodeStut.appendChild(nodeGrade);
XMLUtil.writeToXML(document);
}
public void delete(String name) throws SAXException, IOException, ParserConfigurationException, TransformerException {
Document document = XMLUtil.getDocument();
NodeList list = document.getElementsByTagName("name");
for(int i = 0; i < list.getLength(); i ++) {
if(list.item(i).getTextContent().equals(name)) {
Node node = list.item(i).getParentNode();
node.getParentNode().removeChild(node);
XMLUtil.writeToXML(document);
}
}
}
public Student find(String examid) throws SAXException, IOException, ParserConfigurationException {
Document document = XMLUtil.getDocument();
NodeList stutList = document.getElementsByTagName("student");
for(int i = 0; i < stutList.getLength(); i ++) {
Element elem = (Element)stutList.item(i);
if(elem.getAttribute("examid").equals(examid)) {
Student s = new Student();
s.setName(elem.getElementsByTagName("name").item(0).getTextContent());
s.setExamid(examid);
s.setGrade(Double.parseDouble(elem.getElementsByTagName("grade").item(0).getTextContent()));
s.setIdcard(elem.getAttribute("idcard"));
s.setLocation(elem.getElementsByTagName("location").item(0).getTextContent());
return s;
}
}
return null;
}
}
XMLUtil.java
package utils;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XMLUtil {
public static Document getDocument() throws SAXException, IOException, ParserConfigurationException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document document = factory.newDocumentBuilder().parse("src/exam.xml");
return document;
}
public static void writeToXML(Document document) throws TransformerException {
//更新XML
TransformerFactory tf = TransformerFactory.newInstance();
//得到转换器
Transformer ts = tf.newTransformer();
ts.transform(new DOMSource(document), new StreamResult(new File("src/exam.xml")));
}
}
Main.java
package view;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;
import dao.StudentDao;
import domain.Student;
public class Main {
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException {
System.out.println("添加用户:(a) 删除用户:(b) 查询成绩:(c)");
System.out.println("请输入操作类型:");
Student student = new Student();
StudentDao stuDao = new StudentDao();
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String type = bf.readLine();
if(type.equalsIgnoreCase("a")) {
System.out.println("请输入学生姓名:");
student.setName(bf.readLine());
System.out.println("请输入学生准考证号:");
student.setExamid(bf.readLine());
System.out.println("请输入学生身份证号:");
student.setIdcard(bf.readLine());
System.out.println("请输入学生所在地:");
student.setLocation(bf.readLine());
System.out.println("请输入学生成绩:");
student.setGrade(Double.parseDouble(bf.readLine()));
System.out.println("------添加数据成功------");
stuDao.add(student);
}else if(type.equalsIgnoreCase("b")) {
System.out.println("请输入删除的学生姓名:");
stuDao.delete(bf.readLine());
}else if(type.equalsIgnoreCase("c")) {
System.out.println("请输入查询的学生的准考证号:");
stuDao.find(bf.readLine());
System.out.println("您查询的学生信息为:");
System.out.println("姓名:"+student.getName()+",身份证号:"+student.getIdcard()+",准考证号:"+student.getExamid()+",地区:"+student.getLocation()+",成绩:"+student.getGrade());
}else {
System.out.println("没有");
}
}
}