对于那些比如学生信息的纯文本数据的存取,现在比较流行的是将他进行XML存放,而不是安装数据库去存放这些文字,这也算是目前XML的一个比较好的用途,本文在JAVA自带的XML解析器的基础上,实现了控制台输入命令对XML文档进行增删改查的功能,而且引入了编译异常与运行异常和异常作为返回值的内容,比较耐看,下面将代码分模块贴出。
一。Java Bean 实现对学生信息的面向对象封装
package com.bird.domain;
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 com.bird.dao;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.bid.utils.XmlUtils;
import com.bird.Exception.StudentNotExitException;
import com.bird.domain.Student;
public class StudentDao {
public void add(Student s){
try {
Document document = XmlUtils.getDocument();
//创建出封装学生信息的标签
Element student_tag = document.createElement("student");
//设置属性
student_tag.setAttribute("idcard", s.getIdcard());
student_tag.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(String.valueOf(s.getGrade()));
//设置他们的关系
student_tag.appendChild(name);
student_tag.appendChild(location);
student_tag.appendChild(grade);
//把得到的根节点挂到文档上
document.getElementsByTagName("exam").item(0).appendChild(student_tag);
//更新内存
XmlUtils.write2Xml(document);
} catch (Exception e) {
throw new RuntimeException(e);//将编译异常转换成运行时异常,方便上层想处理就处理不像处理拉到
}
}
public Student find(String examid){
try {
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("student");//获得所有的student节点进行判断
for(int i = 0; i < list.getLength(); i++){
Element student_tag = (Element) list.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;
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
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.write2Xml(document);
return;
}
}
throw new StudentNotExitException(name + "不存在 !!");//编译时异常,要抛出,说明不存在,把异常当作返回值
}catch(StudentNotExitException e){
throw e;
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package com.bid.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 = "d://exam.xml";
public static Document getDocument() throws Exception {// 获得XML的Document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(filename);
}
public static void write2Xml(Document doc) throws Exception {// 将更改后的XML更新到硬盘
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(doc), new StreamResult(new FileOutputStream(
filename)));
}
}
四。需要删除信息不存在的异常,自己编写,作为返回值
package com.bird.Exception;
public class StudentNotExitException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
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
}
}
五。DAO测试代码
package com.bird.test;
import com.bird.Exception.StudentNotExitException;
import com.bird.dao.StudentDao;
import com.bird.domain.Student;
/**
* @测试类
* @author Bird
*
*/
public class DaoTest {
public static void main(String[] args) throws StudentNotExitException {
StudentDao dao = new StudentDao();
@SuppressWarnings("unused")
Student s = new Student();
/*
* s.setExamid("123"); s.setGrade(99); s.setIdcard("31234");
* s.setLocation("上海"); s.setName("鸟"); dao.add(s);
*/
// System.out.println(dao.find("222").getLocation());
dao.delete("张三");
}
}
六。用户使用UI层
package com.bird.ui;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.bird.Exception.StudentNotExitException;
import com.bird.dao.StudentDao;
import com.bird.domain.Student;
/**
* @客户使用程序
* @author Bird
*
*/
public class Main {
public static void main(String [] args){
try{
System.out.println("添加学生(a) 删除学生(b) 查找学生(c)");
System.out.print("请输入操作类型");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String type = br.readLine();
if(type.equals("a")){
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.setName(name);
s.setExamid(examid);
s.setGrade(Double.parseDouble(grade));
s.setIdcard(idcard);
s.setLocation(location);
StudentDao dao = new StudentDao();
dao.add(s);
System.out.println("添加成功");
}else if(type.equals("b")){
System.out.print("请输入要删除的学生的姓名");
String namen = br.readLine();
try{
StudentDao dao = new StudentDao();
dao.delete(namen);
System.out.println("删除成功");
}catch(StudentNotExitException e){
System.out.println("您要删除的学生不存在");
}
}else if(type.equals("c")){
System.out.print("请输入您要查询的学生准考证号");
String num = br.readLine();
StudentDao dao = new StudentDao();
System.out.println("成绩为" + dao.find(num).getGrade());
}else{
System.out.println("对不起,不支持您的操作!!");
}
}catch(Exception e){
e.printStackTrace();
System.out.println("对不起,俺出错了!!!");
}
}
}
七。总结
麻雀虽小五脏俱全,这个小系统的模块划分和异常处理可以作为
以后大程序的典范