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>
Student类
package domain
public class Student {
private String idcard;
private String examid;
private String name;
private String location;
private double grade;
get set方法省略
}
<pre name="code" class="java">StudentDao类
package dao;
import javax.management.RuntimeErrorException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import utils.XmlUtils;
import domain.Student;
import exception.StudentNotExitException;
public class StudentDao {
public void add(Student s){
try {
Document document = XmlUtils.getDocument();
//创建出封装学生信息的标签
Element student_flag = document.createElement("student");
student_flag.setAttribute("idcard", s.getIdcard());
student_flag.setAttribute("examid", s.getExamid());
//创建用于封装学生姓名,所在地,成绩的子标签
Element name = document.createElement("name");
Element location = document.createElement("location");
Element grade = document.createElement("grade");
name.setTextContent(s.getName());
location.setTextContent(s.getLocation());
grade.setTextContent(s.getGrade()+"");
student_flag.appendChild(name);
student_flag.appendChild(location);
student_flag.appendChild(grade);
//把学生信息加到xml上
document.getElementsByTagName("exam").item(0).appendChild(student_flag);
//更新内存
XmlUtils.writetoXml(document);
} catch (Exception e) {
// TODO Auto-generated catch block
throw new RuntimeException(e);//转换为运行时异常
}
}
public Student find(String examid){
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("student");
for(int i=0;i<list.getLength();i++){
Element student_tag = (Element) list.item(i);
if(student_tag.getAttribute("examid").equals(examid)){
//找打相同学生 new一个Student封装输出
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 StudentNotExitException{
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");
for(int i=0;i<list.getLength();i++){
if(list.item(i).getTextContent().equals(name)){
list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
XmlUtils.writetoXml(document);
return;
}
}
throw new StudentNotExitException(name+"不存在!");
} catch (StudentNotExitException e) {
throw e;
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
自己定义的异常类
package exception;
public class StudentNotExitException extends Exception {
public StudentNotExitException() {
// TODO Auto-generated constructor stub
}
public StudentNotExitException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public StudentNotExitException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public StudentNotExitException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
工具类
package utils;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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";
public static Document getDocument() throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document= builder.parse(filename);
return document;
}
public static void writetoXml(Document document) throws Exception{
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer tf = tfactory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
}
}
Main类
package UI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import dao.StudentDao;
import domain.Student;
import exception.StudentNotExitException;
public class Main {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
try{
System.out.println("添加学生(a) 删除学生(b) 查找学生(c)");
System.out.print("请输入操作类型:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String type = br.readLine();
StudentDao dao = new StudentDao();
if("a".equals(type)){
System.out.print("请输入学生姓名");
String name = br.readLine();
System.out.print("请输入学生准考证");
String examid = br.readLine();
System.out.print("请输入学生身份证");
String idcard = br.readLine();
System.out.print("请输入学生所在地");
String location = br.readLine();
System.out.print("请输入学生成绩");
String grade = br.readLine();
Student s = new Student();
s.setExamid(examid);
try{
s.setGrade(Double.parseDouble(grade));
}catch(NumberFormatException e){
System.out.println("输入数据类型异常");
System.exit(0);
}
s.setIdcard(idcard);
s.setLocation(location);
s.setName(name);
dao.add(s);
System.out.println("添加成功");
}else if("b".equals(type)){
System.out.print("请输入要删除的学生姓名");
String name = br.readLine();
try{
dao.delete(name);
System.out.print("已删除!");
}catch(StudentNotExitException e){
System.out.print("不存在");
}
}else if("c".equals(type)){
System.out.print("请输入查找学生的考试号");
String num = br.readLine();
Student s= dao.find(num);
if(s instanceof Student){
System.out.println("找到");
}else {
System.out.println("没找到");
}
}else{
System.out.println("无此操作");
}
}catch(Exception e){
System.out.println("出错!");
e.printStackTrace();
}
}
}