XML当做数据库,完成增删查

XML当做数据库,完成增删查

 


import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.servlet.http.HttpServlet;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Scanner;

//怎么做出算法呢? 而不是踩坑解决
//flag 可以用来判断是否添加成功;把 writer(); 放在 add 方法的最后;addElement 是节点标签,addText是节点文本
public class Hw1 extends HttpServlet {
    private static boolean flag = true;

    public static void main(String[] args) throws IOException {
        Hw1 hw1 = new Hw1();
        hw1.getDoc();

        while (flag){
            System.out.println("a:添加用户;b:查询成绩;c:删除用户");
            System.out.println("请输入操作类型:");
            String option = sc.next();
            switch (option){
                case "a" :
                    add();
                    break;
                case "c":
                    delete();
                    break;
                case "b":
                    select();
                    break;
            }
        }
    }

    static Scanner sc = new Scanner(System.in);
    private static Document doc;

    private void getDoc (){
        try {
            SAXReader sr = new SAXReader();
            doc = sr.read(new File("src/day11/hw.xml"));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void add() throws IOException {
        System.out.println("请输入身份证号");
        String s1 = sc.nextLine();
        s1 = sc.nextLine();
        System.out.println("请输入准考证号");
        String s2 = sc.nextLine();
        System.out.println("请输入学生姓名");
        String s3 = sc.nextLine();
        System.out.println("请输入所在地");
        String s4 = sc.nextLine();
        System.out.println("请输入成绩");
        String s5 = sc.nextLine();

        Element elementRoot = doc.getRootElement();

        Element elementBrand1 = elementRoot.addElement("Student");   //Student类型
        elementBrand1.addAttribute("idcard", s1);  //属于 Student 的属性
        elementBrand1.addAttribute("examid", s2);  //属于 Student 的属性
        Element elementBrand2 = elementBrand1.addElement("name"); //Student子节点
        elementBrand2.addText(s3);  //子节点内容
        Element elementBrand3 = elementBrand1.addElement("location");
        elementBrand3.addText(s4);
        Element elementBrand4 = elementBrand1.addElement("grade");
        elementBrand4.addText(s5);
        writer();
        flag = false;
    }
//    写入
    public static void writer() throws IOException {
        FileOutputStream fis = new FileOutputStream("7.xml");
        OutputFormat out = OutputFormat.createPrettyPrint();
        out.setEncoding("utf-8");

        XMLWriter writer = new XMLWriter(fis,out);
        writer.write(doc);
    }

    public static void delete() throws IOException {
        System.out.println("请输入删除的姓名");
        String s1 = sc.nextLine();
        s1 = sc.nextLine();

        Element elementRoot = doc.getRootElement();
        Iterator<Element> iterator = elementRoot.elementIterator();
        System.out.println(1);
        while (iterator.hasNext()){
            Element elementBrand = iterator.next();
//            if (elementBrand.attributeValue("name").equals("s1")){

            if (elementBrand.element("name").getText().equals(s1)){
                elementBrand.getParent().remove(elementBrand);
                System.out.println("2\t");
                writer();
                flag = false;

            }
        }
    }

    //查询
    public static void select(){
        System.out.println("请输入查询的准考证号");
        String s1 = sc.nextLine();
        s1 = sc.nextLine();

        Element elementRoot = doc.getRootElement();  //根节点
        Iterator<Element> iterator = elementRoot.elementIterator();
        while (iterator.hasNext()){
            Element elementBrand = iterator.next();  //Student节点

            if (elementBrand.attributeValue("examid").equals(s1)){

                String name = elementBrand.element("name").getText();
                String location = elementBrand.element("location").getText();
                String grade = elementBrand.element("grade").getText();
                System.out.println("姓名:"+name+"\t身份证号:"+elementBrand.attributeValue("idcard")
                        +"\t准考证号:"+elementBrand.attributeValue("examid")+"\t地区:"+location+"\t成绩:"+grade);
                flag = false;
            }
        }
    }
}

 

结果展示:


1添加
 


2查询
 


3删除前
 


4删除后
 

posted @ 2020-08-11 23:06  ping4  阅读(321)  评论(0编辑  收藏  举报