简单的考生管理系统,java以XML为数据库,jaxp操作XML实现简单的增删查功能

运行界面控制台界面:
这里写图片描述
项目目录结构:
这里写图片描述
其中bean目录封装数据,dao目录进行业务处理,utils目录是封装的读取和保存xml的工具类,test目录是junit测试类,ui目录是main类。
XML测试文档数据库:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>

    <student examid="123" idcard="2222">
        <name>ceshi</name>
        <location>北京</location>
        <grade>344.0</grade>
    </student>
</exam>

Student.java代码:

package com.cx.bean;

public class Student {

    private String name;
    private String idcard;
    private String examid;
    private String location;
    private double grade;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    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 getLocation() {
        return location;
    }
    public void setLocation(String location) {
        this.location = location;
    }
    public double getGrade() {
        return grade;
    }
    public void setGrade(double grade) {
        this.grade = grade;
    }

}

StudentDao.java

package com.cx.dao;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.cx.bean.Student;
import com.cx.utils.XMLUtils;

public class StudentDao {

    //实现对数据的增删查功能

    //增加学生信息
    public void add(Student s) throws Exception{

        //首先获取xml的对象
        Document document = XMLUtils.getDocument();

        //创建student节点
        Element student_tag = document.createElement("student");
        //为student节点添加属性和属性值
        student_tag.setAttribute("idcard", s.getIdcard());
        student_tag.setAttribute("examid", s.getExamid());

        //创建student的子节点
        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()+"");//这里让double转化为String

        //增加节点
        student_tag.appendChild(name);
        student_tag.appendChild(location);
        student_tag.appendChild(grade);

        document.getElementsByTagName("exam").item(0).appendChild(student_tag);

        //保存
        XMLUtils.weite2XML(document);

    }

    //根据学生准考证号删除学生信息

    public void delete(String examid) throws Exception{

        Document document = XMLUtils.getDocument();

        //获得student节点
        NodeList list = document.getElementsByTagName("student");
        for(int i=0;i<list.getLength();i++){
            Element e = (Element) list.item(i);
            if(examid.equals(e.getAttribute("examid"))){
                e.getParentNode().removeChild(e);
                XMLUtils.weite2XML(document);
            }
        }

    }


    //根据学生的姓名查找学生,并返回学生信息
    public Student find(String name) throws Exception{

        Document document = XMLUtils.getDocument();

        NodeList list = document.getElementsByTagName("name");

        for(int i=0;i<list.getLength();i++){
            if(list.item(i).getTextContent().equals(name)){
                Element e = (Element) list.item(i).getParentNode();
                Student s = new Student();
                s.setExamid(e.getAttribute("examid"));
                s.setGrade(Double.parseDouble(e.getElementsByTagName("grade").item(0).getTextContent()));
                s.setIdcard(e.getAttribute("idcard"));
                s.setLocation(e.getElementsByTagName("location").item(0).getTextContent());
                s.setName(e.getElementsByTagName("name").item(0).getTextContent());

                return s;
            }
        }
        return null;
    }
}

testStudent.java

package com.cx.test;

import org.junit.Test;

import com.cx.bean.Student;
import com.cx.dao.StudentDao;

public class TestStudentDao {

    @Test
    public void testAdd() throws Exception{
        StudentDao dao = new StudentDao();
        Student s = new Student();

        s.setExamid("14");
        s.setGrade(87);
        s.setIdcard("888");
        s.setName("王五");
        s.setLocation("上海");

        dao.add(s);
    }

    @Test
    public void testDelete() throws Exception{
        StudentDao dao = new StudentDao();
        dao.delete("14222");
    }

    @Test
    public void testFind() throws Exception{
        StudentDao dao = new StudentDao();
        Student ss = dao.find("张三1");
    }

}

Main.java

package com.cx.ui;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.Buffer;

import com.cx.bean.Student;
import com.cx.dao.StudentDao;

public class Main {

    /**
     * @param args
     */
    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("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);
                s.setGrade(Double.parseDouble(grade));
                s.setIdcard(idcard);
                s.setLocation(location);
                s.setName(name);

                StudentDao dao = new StudentDao(); 
                dao.add(s);

            }else if("b".equals(type)){

                System.out.print("请输入要删除学生的准考证号:");
                String examid = br.readLine();

                StudentDao dao = new StudentDao(); 
                dao.delete(examid);


            }else if("c".equals(type)){
                System.out.print("请输入要查询学生的姓名:");
                String name = br.readLine();

                StudentDao dao = new StudentDao(); 
                Student s = dao.find(name);
                if(s!=null){
                    System.out.println("姓名:"+name);
                    System.out.println("考号:"+s.getExamid());
                    System.out.println("身份证号:"+s.getIdcard());
                    System.out.println("地址:"+s.getLocation());
                    System.out.println("分数:"+s.getGrade());
                }else{
                    System.out.println("查无此人!!!");
                }


            }else{
                System.out.println("您输入的有错误!");
            }
        }catch(Exception e){
            e.printStackTrace();
            System.out.println("正忙。。。请稍候!!!");
        }

    }

}

XMLUtils.java

package com.cx.utils;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
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 {
    //操作XML的工具类,属性和方法最好都定义为静态的static,这样就可以直接用类名调用方法

    //定义xml文件地址
    private static String filename = "src/student.xml"; 

    //返回Document对象的方法
    public static Document getDocument() throws Exception{

        //首先获取工厂类
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        //获取解析器
        DocumentBuilder  builder = factory.newDocumentBuilder();

        //解析xml文档
        Document document = builder.parse(filename);

        return document;
    }

    //更新xml,将内存内容写入并保存到xml中
    public static void weite2XML(Document document) throws Exception{
        //获得转换器工厂类
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer tf = factory.newTransformer();
        tf.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filename)));
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值