XML动物园

1 篇文章 0 订阅
这篇博客介绍了如何使用DOM4J库结合面向对象编程,将动物园中的动物信息(包括3头大象和3只猴子的具体详情)转换并保存为XML文件,帮助管理员实现数据持久化。
摘要由CSDN通过智能技术生成

1.动物园新进了3头大象:

​ 胖胖,2岁,1.2吨;

​ 肥仔,1岁,1.5吨;

​ 憨憨,3岁,1.8吨;

还有3只猴子:

​ 星仔,3岁,0.8米;

​ 狒狒,4岁,0.9米;

​ 猴哥,5岁,1.0米;

它们的信息存在了集合中,现在动物管理员需要将这些宝贝们的信息存入xml中保存.请你写一个程序,结合面向对象,和dom4j技术,帮管理员实现这个功能。
Animal类

public class Animal {
    private String name;
    private int age;

    public Animal() {
    }

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

Elephant类

public class Elephant extends Animal {
    private double weight;

    public Elephant() {
    }

    public Elephant(double weight) {
        this.weight = weight;
    }

    public Elephant(String name, int age, double weight) {
        super(name, age);
        this.weight = weight;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }
}

Monkey类

 private double height;

    public Monkey() {
    }

    public Monkey(double height) {
        this.height = height;
    }

    public Monkey(String name, int age, double height) {
        super(name, age);
        this.height = height;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }
}

Test1类

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.util.ArrayList;

public class Test1 {
    public static void main(String[] args) throws Exception {
        ArrayList<Animal> animals = new ArrayList<>();
        animals.add(new Elephant("胖胖",2,1.3));
        animals.add(new Elephant("肥仔",1,1.5));
        animals.add(new Elephant("憨憨",3,1.8));
        animals.add(new Monkey("星仔",3,0.8));
        animals.add(new Monkey("狒狒",4,0.9));
        animals.add(new Monkey("猴哥",5,1.0));
        createXml(animals);
    }

    public static void createXml(ArrayList<Animal> animals) throws Exception {
        //创建一个dom树
        Document doc = DocumentHelper.createDocument();
        //添加根节点
        Element ani = doc.addElement("animals");
        //遍历集合
        for (int i = 0; i < animals.size(); i++) {
            //添加子节点
            Element a = ani.addElement("animal");
            //给子节点添加子节点
            Element name = a.addElement("name");
            name.setText(animals.get(i).getName());
            Element age = a.addElement("age");
            //age.setText(Integer.toString(animals.get(i).getAge()));
            //age.setText(String.valueOf(animals.get(i).getAge()));
            age.setText(animals.get(i).getAge()+"岁");
            //判断 是大象还是猴子
            if(animals.get(i) instanceof Elephant){
                Element weight = a.addElement("weight");
                weight.setText(((Elephant) animals.get(i)).getWeight()+"吨");
            }else {
                Element height = a.addElement("height");
                height.setText(((Monkey) animals.get(i)).getHeight()+"米");
            }
        }
        //写入xml中
        FileOutputStream out = new FileOutputStream("D:\\IdeaworkSpace\\day22-java22\\src\\animal");
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(doc);
        writer.close();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值