zoukankan      html  css  js  c++  java
  • xml+dom4j+xpath学生管理系统

    <?xml version="1.0" encoding="UTF-8"?>
    
    <student> 
      <stu> 
        <name>张三</name>  
        <age>16</age>  
        <sex></sex> 
      </stu>  
      <stu> 
        <name>李四</name>  
        <age>18</age>  
        <sex></sex> 
      </stu>  
      <stu> 
        <name>王五</name>  
        <age>17</age>  
        <sex></sex> 
      </stu> 
    </student>
    package cn.itcast.vo;
    
    public class Student {
        private String name;
        private int age;
        private String sex;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public String toString(){
            return name+":"+age+":"+sex;
        }
    }
    package student;
    
    import java.io.FileOutputStream;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    
    import cn.itcast.vo.Student;
    
    public class StuServer {
        //增加
        public static void addStu(Student s) throws Exception{
            SAXReader sax = new SAXReader();
            Document document = sax.read("src/student.xml");
            Element root = document.getRootElement();
            Element stu = root.addElement("stu");
            Element name = stu.addElement("name");
            Element age = stu.addElement("age");
            Element sex = stu.addElement("sex");
            name.setText(s.getName());
            age.setText(s.getAge()+"");
            sex.setText(s.getSex());
            //回写
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter  xmlwriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
            xmlwriter.write(document);
            xmlwriter.close();
        }
        //删除  根据学生的id
        public  static void delStu(String name)throws Exception{
            SAXReader sax = new SAXReader();
            Document document = sax.read("src/student.xml");
            List<Node> list=document.selectNodes("//name");
            for(Node node:list){
                String namev = node.getText();
                if(namev.equals(name)){
                    Element stu = node.getParent();
                    Element student = stu.getParent();
                    student.remove(stu);
                }
            }
            //回写
            OutputFormat format = OutputFormat.createPrettyPrint();
            XMLWriter  xmlwriter = new XMLWriter(new FileOutputStream("src/student.xml"),format);
            xmlwriter.write(document);
            xmlwriter.close();
        }
        public static Student selectStu(String name)throws Exception{
            SAXReader sax = new SAXReader();
            Document document = sax.read("src/student.xml");
            List<Node>list = document.selectNodes("//name");
            Student s = new Student();
            for(Node node : list){
                String namev = node.getText();
                if(namev.equals(name)){
                    Element stu = node.getParent();
                    int agev =Integer.parseInt(stu.element("age").getText());
                    String sexv = stu.element("sex").getText();
                    s.setAge(agev);
                    s.setName(namev);
                    s.setSex(sexv);
                }
            }
            return s;
        }
    }
    package cn.itcast.test;
    
    import cn.itcast.vo.Student;
    import student.StuServer;
    
    public class Test {
    
        public static void main(String[] args) throws Exception{
    //        addTest();
    //        delTest();
            selectTest();
    
        }
        public static void  addTest() throws Exception{
            Student s = new Student();
            s.setName("tom");
            s.setAge(20);
            s.setSex("男");
            StuServer.addStu(s);
        }
        public  static void delTest()throws Exception{
            StuServer.delStu("tom");
        }
        public static void selectTest()throws Exception{
            Student student = StuServer.selectStu("王五");
            System.out.println(student.toString());
        }
    
    }
  • 相关阅读:
    BZOJ1912 [Apio2010]patrol 巡逻
    BZOJ2432 [Noi2011]兔农
    BZOJ1010 [HNOI2008]玩具装箱toy
    BZOJ3240 [Noi2013]矩阵游戏
    洛谷【P1303】A*B Problem
    洛谷【2142】高精度减法
    洛谷【P1100】高低位交换
    OIer应该知道的二进制知识
    洛谷【P3908】异或之和
    洛谷【P2431】正妹吃月饼
  • 原文地址:https://www.cnblogs.com/wangyinxu/p/7402316.html
Copyright © 2011-2022 走看看