日常练习-xml成绩管练习

前端之家收集整理的这篇文章主要介绍了日常练习-xml成绩管练习前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package dao;

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



import domain.Student;
import exception.StudentNotExistException;

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(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) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);//unchecked exception
        }
    }

    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)){
                    //找到了匹配的学生
                    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("grade").item(0).getTextContent());
                    s.setGrade(Double.parseDouble(student_tag.getElementsByTagName("grade").item(0).getTextContent()));
                    return s;
                }
            }
            return null;



        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        }
    }

    public void delete(String name) throws StudentNotExistException{
        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 StudentNotExistException(name+"is not exist!");


        }catch(StudentNotExistException e){
            throw  e;
        }

        catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        }
    }

}
package 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;
    }


}
package exception;

public class StudentNotExistException extends Exception {

    public StudentNotExistException() {
        // TODO Auto-generated constructor stub
    }

    public StudentNotExistException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public StudentNotExistException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }

    public StudentNotExistException(String message,Throwable cause) {
        super(message,cause);
        // TODO Auto-generated constructor stub
    }

    public StudentNotExistException(String message,Throwable cause,boolean enableSuppression,boolean writableStackTrace) {
        super(message,cause,enableSuppression,writableStackTrace);
        // TODO Auto-generated constructor stub
    }

}
package test;

import org.junit.Test;

import dao.StudentDao;
import domain.Student;
import exception.StudentNotExistException;

public class StudentDaoTest {

    @Test
    public void testAdd(){
        StudentDao dao = new StudentDao();
        Student s = new Student();
        s.setExamid("121");
        s.setGrade(89);
        s.setIdcard("121");
        s.setLocation("北京");
        s.setName("aa");
        dao.add(s);
    }

    @Test
    public void testFind(){
        StudentDao dao = new StudentDao();
        dao.find("121");
    }

    @Test
    public void testDelelte() throws StudentNotExistException{
        StudentDao dao = new StudentDao();
        dao.delete("aa");
    }
}
package ui;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import dao.StudentDao;
import domain.Student;
import exception.StudentNotExistException;

public class Main {

    public static void main(String[] args) {
        System.out.println("添加用户(a) 删除用户(b) 查找学生");
        System.out.print("请输入操作类型:");

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            String type =br.readLine();

            if("a".equals(type)){
                System.out.println("请输出姓名");
                String name = br.readLine();
                System.out.println("请输出准考证号");
                String examid = br.readLine();
                System.out.println("请输出身份证号码");
                String idcard = br.readLine();
                System.out.println("请输出所在地");
                String location = br.readLine();
                System.out.println("请输出成绩");
                String grade = br.readLine();

                Student s = new Student();
                s.setName(name);
                s.setIdcard(idcard);
                s.setExamid(examid);
                s.setLocation(location);
                s.setGrade(Double.parseDouble(grade));

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

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


                System.out.println("请输入删除学生");
                String name = br.readLine();

                StudentDao dao = new StudentDao();
                try {
                    dao.delete(name);
                    System.out.println("删除成功");
                } catch (StudentNotExistException e) {
                    // TODO Auto-generated catch block
                    System.out.println("删除用户不存在");
                }
            }else if("c".equals(type)){

            }else{
                System.out.println("不支持的操作");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("输入出错");
        }
    }

}
package utils;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

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.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;

import com.sun.xml.internal.bind.unmarshaller.DOMScanner;

public class XmlUtils {

    private static String filename = "src/exam.xml";
    public static Document getDocument() throws ParserConfigurationException,SAXException,IOException{
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();

        return builder.parse(filename);

    }

    public static void write2Xml(Document docum) throws FileNotFoundException,TransformerException{
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer tf = factory.newTransformer();
        tf.transform(new DOMSource(docum),new StreamResult(new FileOutputStream(filename)));
    }
}
<?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>

猜你在找的XML相关文章