Using Xml and reflection to realize pet management system in Java

1. Pet Management System

1. Code

Main interface code of pet management system:

@Test//宠物管理系统
    public void admin() throws Exception {
    
    
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择功能:1.领养宠物  2.捐献宠物");
        if(sc.nextInt()==1){
    
    //领养
            adoptPet();
        }else{
    
    //捐献
            donatePet();
        }

    }

Donation pet function code:

 @Test//捐献功能
    public void donatePet() throws Exception {
    
    
        String animalType = getAnimalType();
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择您要捐献的宠物类型:"+animalType+",或其它类型");
        String petType = sc.next();
        Document doc = getDoc();
        Element root = doc.getRootElement();
        Element animal;
        if(animalType.contains(petType)){
    
    //捐献已经有的宠物
            animal= root.element(petType);
        }else {
    
     //捐献新物种
             animal = root.addElement(petType);
        }
        System.out.println("请输入宠物的名字:");
        String name = sc.next();
        animal.addElement("name").setText(name);

        //写入xml并输出
        writeAndPrintXml(doc);
    }

Feature code for adopting pets:

 @Test//领养功能----领养宠物种类动态化,并删除xml中对应的宠物标签
    public void adoptPet() throws Exception {
    
    
        //从xml中获取宠物的种类
        String animals=getAnimalType();
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择您要领养的宠物类型:"+animals);
        String name = sc.next();
        //删除对应的宠物标签
        Document doc = getDoc();
        Element root = doc.getRootElement();
        // 查找根节点下的子节点 element() elements()
        Element animal = root.element(name);
        List <Element> name1 = animal.elements("name");
        String names="";
        for (int i = 0; i < name1.size(); i++) {
    
    
            names+=name1.get(i).getText();
            if(i < name1.size()-1){
    
    
                names+=",";
            }
        }
        System.out.println("请选择您要领养的宠物名称:"+names);
        String petName = sc.next();
        for (int i = 0; i < name1.size(); i++) {
    
    
            if(petName.equals(name1.get(i).getText()) ){
    
    
                //删除
                animal.remove(name1.get(i));
                break;
            }
        }
        //写入xml并输出
        writeAndPrintXml(doc);
    }

Encapsulate the new dom tree to cover the original dom tree method code:

//封装方法--新的dom覆盖原来的dom,并在控制台输出
    public void writeAndPrintXml(Document doc) throws Exception {
    
    
        //新的dom树覆盖旧的dom树
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/work/two/animal.xml"), OutputFormat.createPrettyPrint());
        xmlWriter.write(doc);
        xmlWriter.close();

        //dom树在控制台输出
        XMLWriter xmlWriter2 = new XMLWriter(System.out, OutputFormat.createPrettyPrint());
        xmlWriter2.write(doc);
        xmlWriter2.close();
    }

The method code of encapsulating the root node:

//封装一个获取根节点的方法
    public Element getRoot()throws Exception{
    
    
        // 1.创建解析器
        SAXReader reader = new SAXReader();
        // 解析xml文档,得到document对象
        Document document = reader.read("src/work/two/animal.xml");
        // 根据document对象获取根节点
        return document.getRootElement();
    }

Encapsulate and obtain a new dom tree code:

//封装一个获取dom树
    public Document getDoc()throws Exception{
    
    
        // 1.创建解析器
        SAXReader reader = new SAXReader();
        // 解析xml文档,得到document对象
        return reader.read("src/work/two/animal.xml");

    }

Get the pet category code from the Xml document:

//从xml中获取宠物的种类,并返回一个字符串
    private String getAnimalType() throws DocumentException {
    
    
        // 1.创建解析器
        SAXReader reader = new SAXReader();
        // 解析xml文档,得到document对象
        Document document = reader.read("src/work/two/animal.xml");
        // 根据document对象获取根节点
        Element root = document.getRootElement();
        List<Element> animals = root.elements();
        String names="";
        for (int i = 0; i < animals.size(); i++) {
    
    
            names+=animals.get(i).getName();
            if(i<animals.size()-1)
                names+=",";
        }
        return names;
    }

The original version of the adopted pet code:

@Test//原始版本
    public void test1() throws Exception {
    
    
        Scanner sc=new Scanner(System.in);
        System.out.println("请选择您要领养的宠物:狗狗,企鹅,大象");
        String name = sc.next();
        Animal animal = getAnimal(name);
        System.out.println(animal);
    }

Return the object code according to the type of pet:

//根据宠物类型名,返回一个对象
    public Animal getAnimal(String name) throws Exception {
    
    
        // 1.创建解析器
        SAXReader reader = new SAXReader();
        // 解析xml文档,得到document对象
        Document document = reader.read("src/work/two/animal.xml");
        // 根据document对象获取根节点
        Element root = document.getRootElement();
        // 查找根节点下的子节点 element() elements()
        Element animal = root.element(name);
        String className = animal.element("class").getText();
        //2.使用反射创建对象
        return (Animal) Class.forName(className).newInstance();
    }
}

Insert picture description here

Insert picture description here

to sum up

The above is the entire content of today's use of xml and reflection to realize the pet management system, mainly using the function of adding and deleting xml.

Guess you like

Origin blog.csdn.net/StruggleBamboo/article/details/112031991