Xml文档—exam.xml
<?xml version="1.0" encoding="UTF-8"?>
<exam>
<student examid="111" idcard="111123123">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student examid="222" idcard="22212342323">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
程序实现:用户对xml文档的添加、删除、查询。
封装数据的Student类
package yyy.exam.domain;
public class Student {
private String examid;
private String idcard;
private String name;
private String location;
private double grade;
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getIdcard() {
return idcard;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
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;
}
@Override
public String toString() {
return "学生姓名:"+name+"/r/n准考证号:"+examid+"/r/n身份证号:"+idcard+"/r/n家庭住址:"+location+"/r/n成绩:"+grade;
}
}
工具类—XmlUtils
package yyy.exam.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XmlUtils {
private static File file = new File("src/exam.xml");
public static Document getDocument() throws DocumentException{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
return document;
}
public static void write2Xml(Document document) throws IOException{
OutputFormat format = OutputFormat.createPrettyPrint(); //特别要注意乱码问题
XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);
writer.write(document);
writer.close();
}
}
方法类—StudentDao
package yyy.exam.dao;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import yyy.exam.domain.Student;
import yyy.exam.utils.XmlUtils;
public class StudentDao {
public static void add(Student student){
try {
Document document = XmlUtils.getDocument(); //获得document对象
Element root = document.getRootElement(); //获得跟节点
Element student_node = root.addElement("student"); //添加名为student的新节点
student_node.addAttribute("examid", student.getExamid()); //为student节点添加属性
student_node.addAttribute("idcard", student.getIdcard());
student_node.addElement("name").setText(student.getName()); //为student节点添加子节点
student_node.addElement("location").setText(student.getLocation());
student_node.addElement("grade").setText(student.getGrade()+""); //任何对象和字符串相连都是字符串
XmlUtils.write2Xml(document); //写入xml文档
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static int delete(String examid){
try {
int count = 0; //定义count记录所删除的学生信息个数
Document document = XmlUtils.getDocument();
List<Element> list = document.selectNodes("//student[@examid]"); //获得所有包含examid属性的student元素的集合 Xpath技术
for(Element node : list){
if(node.attributeValue("examid").equals(examid)){
node.getParent().remove(node);
XmlUtils.write2Xml(document);
count++;
}
}
return count;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static ArrayList<Student> find(String name){
try {
ArrayList<Student> students = new ArrayList<Student>(); //创建ArrayList集合用于存储查找到的student对象
Document document = XmlUtils.getDocument();
List<Node> list = document.selectNodes("//name"); //获得所有的name节点 Xpath技术
for(Node node : list){
if(node.getText().equals(name)){
Student student = new Student();
student.setName(name);
student.setExamid(node.getParent().attributeValue("examid"));
student.setGrade(Double.parseDouble(node.getParent().elementText("grade")));
student.setIdcard(node.getParent().attributeValue("idcard"));
student.setLocation(node.getParent().elementText("location"));
students.add(student);
}
}
return students; //返回包含所查到的所有student对象的ArrayList集合
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
用户界面—主运行类—Main
package yyy.exam.UI;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import yyy.exam.dao.StudentDao;
import yyy.exam.domain.Student;
public class Main {
private static Student student = new Student();
private static StudentDao dao = new StudentDao();
public static void main(String[] args) {
Operation();
}
private static void Operation() {
try {
System.out.println("请选择操作类型: 添加学生信息:(a) 删除学生信息:(b) 查看学生信息:(c) 退出;(quit)");
System.out.print("请选择操作类型:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String type = br.readLine(); //定义type记录用户选择的操作类型
if("a".equals(type)){
addStudent(br);
}else if("b".equals(type)){
deleteStudent(br);
return;
}else if("c".equals(type)){
findStudent(br);
return;
}else if("quit".equals(type)){
System.out.println("谢谢使用,欢迎下次光临!!!!");
return;
}else{
System.out.println("选择操作类型错误,请重新选择!!");
System.out.print("请选择操作类型:");
Operation();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("程序出现未知错误,请致电1384389438!!!");
}
}
private static void findStudent(BufferedReader br) {
try {
System.out.print("请输入学生姓名:");
String name = br.readLine();
ArrayList<Student> list = dao.find(name);
if(list.size()>0){ //不能用"list==null"做条件,因为list不为空。
int count = 1; //定义count记录list集合中存储的学生信息个数
System.out.println("共找到"+list.size()+"个符合条件的学生信息");
for(Student student : list){ //遍历list集合打印学生信息
System.out.println();
System.out.println("学生"+count+":");
System.out.println(student);
count++;
}
}else{
System.out.println("没有此学生信息,请检查姓名是否输入正确!!!");
}
System.out.println("yes:继续查找 no:返回上一层");
String type = br.readLine();
if("yes".equals(type))
findStudent(br); //用户输入yes继续该操作
else if("no".equals(type))
Operation(); //用户输入no返回上层继续操作
else
System.out.println("输入错误,你个傻B!!!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("程序出现未知错误,请致电1384389438!!!");
}
}
private static void deleteStudent(BufferedReader br) {
try {
System.out.print("请输入学生准考证号:");
String examid = br.readLine();
int count = dao.delete(examid); //count记录所删除的学生信息的个数
if(count!=0){
System.out.println("删除学生信息成功!!");
System.out.println("共删除"+count+"个准考证号为"+examid+"的学生信息");
}else{
System.out.println("没有此学生信息,请检查准考证号是否输入正确!!!");
}
System.out.println("yes:继续删除 no:返回上一层");
String type = br.readLine();
if("yes".equals(type))
deleteStudent(br); //用户输入yes继续该操作
else if("no".equals(type))
Operation(); //用户输入no返回上层继续操作
else
System.out.println("输入错误,你个傻B!!!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("程序出现未知错误,请致电1384389438!!!");
}
}
private static void addStudent(BufferedReader br) {
try {
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.setExamid(examid);
student.setGrade(Double.parseDouble(grade));
student.setIdcard(idcard);
student.setLocation(location);
student.setName(name);
dao.add(student);
System.out.println("添加学生信息成功");
System.out.println("yes:继续添加 no:返回上一层");
String type = br.readLine();
if("yes".equals(type))
addStudent(br); //用户输入yes继续该操作
else if("no".equals(type))
Operation(); //用户输入no返回上层继续操作
else
System.out.println("输入错误,你个傻B!!!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("程序出现未知错误,请致电1384389438!!!");
}
}
}