JavaWeb学习笔记-XML-4

来源:互联网 发布:如何在淘宝网上开网店 编辑:程序博客网 时间:2024/06/05 06:34

jaxp


学生管理系统
运用xml操作,对学生信息进行增删改查
代码结构
代码结构

StudentDao.java

public class StudentDao {    public void add(Student s){        try {            Document document = XmlUtils.getDocument();            Element studnet_tag = document.createElement("student");            studnet_tag.setAttribute("idcard",s.getIdcard());            studnet_tag.setAttribute("examid",s.getExamid());            Element name = document.createElement("name");            Element locatoion = document.createElement("location");            Element grade = document.createElement("grade");            name.setTextContent(s.getName());            locatoion.setTextContent(s.getLocation());            grade.setTextContent(s.getGrade()+"");            studnet_tag.appendChild(name);            studnet_tag.appendChild(locatoion);            studnet_tag.appendChild(grade);            document.getElementsByTagName("exam").item(0).appendChild(studnet_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");            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 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);                }            }            throw new StudentNotExistException(name+"不存在");        } catch (StudentNotExistException e){            throw e;        }catch (Exception e) {            throw new RuntimeException(e);        }    }}

Student.java

public class Student{    private String idcard;    private String examid;    private String name;    private String location;    private double grade;    public String getIdcard(){        return this.idcard;    }    public double getGrade() {        return grade;    }    public String getExamid() {        return examid;    }    public String getLocation() {        return location;    }    public String getName() {        return name;    }    public void setExamid(String examid) {        this.examid = examid;    }    public void setGrade(double grade) {        this.grade = grade;    }    public void setIdcard(String idcard) {        this.idcard = idcard;    }    public void setLocation(String location) {        this.location = location;    }    public void setName(String name) {        this.name = name;    }}

StudentNotException.java

public class StudentNotExistException extends Exception {    public StudentNotExistException() {        super();    }    public StudentNotExistException(String s) {        super(s);    }    public StudentNotExistException(String message, Throwable cause) {        super(message, cause);    }    public StudentNotExistException(Throwable cause) {        super(cause);    }    protected StudentNotExistException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {        super(message, cause, enableSuppression, writableStackTrace);    }}

Main.java

public class Main {    public static void main(String[] args){        System.out.println("添加学生(a) 删除学生(b) 查找学生(c)");        System.out.println("请输入操作类型:");        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.setIdcard(idcard);                s.setName(name);                s.setLocation(location);                s.setGrade(Double.parseDouble(grade));                s.setExamid(examid);                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) {                    System.out.println("学生不存在");                }            }else if("c".equals(type)){            }else{                System.out.println("不支持此操作");            }        } catch (IOException e) {            e.printStackTrace();            System.out.println("对不起,系统出错");        }    }}

XmlUtils.java

public class XmlUtils {    private static String fileNmae = "src/exam.xml";    public static Document getDocument() throws IOException, SAXException, ParserConfigurationException {        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();        DocumentBuilder builder = factory.newDocumentBuilder();        return builder.parse(fileNmae);    }    public static void write2Xml(Document documnet) throws TransformerException, FileNotFoundException {        TransformerFactory factory = TransformerFactory.newInstance();        Transformer tf = factory.newTransformer();        tf.transform(new DOMSource(documnet),new StreamResult(new FileOutputStream(fileNmae)));    }}

exam.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>    <student examid="222" idcade="111">        <name>张三</name>        <location>沈阳</location>        <garde>89</garde>    </student>    <student examid="444" idcade="333">        <name>李四</name>        <location>大连</location>        <garde>97</garde>    </student>    <student examid="121" idcard="">        <name>aa</name>        <location>北京</location>        <grade>89.0</grade>    </student></exam>
原创粉丝点击