Related methods and cases of Xml in Java

One, Xml

1. Reading method of Xml document

public class Test1 {
    
    
    @Test//查询方法一
    public void test1() throws Exception {
    
    
        //1.创建一个解析器
        SAXReader reader=new SAXReader();
        //2.读取某个xml文件,返回xml对应的dom文档树
        Document doc=reader.read("src\\xml\\msg.xml");
        //3.获得根标签
        Element root=doc.getRootElement();
        System.out.println(root.getName());
        //4.1 获取所有子标签
        List<Element> msgs=root.elements();
        for (Element msg : msgs) {
    
    
            System.out.println(msg.getName());
        }
        //4.1 获取第一个子标签--2种方式
        Element e=msgs.get(0);
        System.out.println(e.getName());
        Element ee=root.element("msg");
        System.out.println(ee.getName());
        //4.2使用迭代器获得子标签
        Iterator it = e.elementIterator();
        while (it.hasNext()){
    
    
            Element e1=(Element) it.next();
            System.out.println(e1.getName()+"--"+e1.getText());
        }
    }
    @Test//查询方法二
    public void test2() throws Exception {
    
    
        //1.获取sax解析器对象
        SAXReader reader = new SAXReader();
        //2.读取某个xml文件--返回一个文档对象,表示整个xml文档树
        Document doc = reader.read("src/xml1/a.xml");
        //3.获取根标签
        Element root = doc.getRootElement();
        //4.逐级向下寻找标签
        Element family1 = (Element) root.elements("family").get(0);
        Element person1 = (Element)family1.elements("person").get(0);
        Element age = person1.element("age");
        System.out.println(age.getText());//获取标签的文本内容
    }
}

2. The addition of tags on the Xml document

 @Test//新增(在原来的dom树上新增)
    public void test2() throws Exception{
    
    
    //一、构建文档树
        //1.创建一个解析器
        SAXReader reader=new SAXReader();
        //2.读取某个xml文件,返回xml对应的dom文档树
        Document doc=reader.read("src/xml/msg.xml");
        //3.获得根标签
        Element root = doc.getRootElement();
        //4.给根标签加一个子标签
        Element msg3 = root.addElement("msg3");
        Element content = msg3.addElement("content");
        content.setText("元旦快乐!");

    //二、用新的文档树覆盖旧的文档树
        //1.借助字节输出流
        FileOutputStream out = new FileOutputStream("src/xml/msg.xml");
        //2.需要一个格式美化对象
        OutputFormat format=OutputFormat.createPrettyPrint();
        //3.根据上面的2个对象,构建一个xml输出流对象
        XMLWriter writer = new XMLWriter(out,format);
        //4.将doc文档树 重新写入 xml文件中
        writer.write(doc);
        //5.关闭流--刷新缓冲区
        writer.close();
    }

3. Create a new Xml document to add tags and content

@Test//新增--场景2(创建新的dom树)
    public void test3() throws Exception{
    
    
        //1.创建一个文档对象--dom树
        Document doc= DocumentHelper.createDocument();
        //2.添加根元素
        Element root = doc.addElement("heroes");
        //3.添加子元素
        Element hero = root.addElement("hero");
        Element name = hero.addElement("name");
        Element age = hero.addElement("age");
        //给子元素设置文本
        name.setText("曹操");
        age.setText("26");

        //将上面创建的dom树,写进一个xml文件中       字节输出流--指定xml文件路径                      美化器,规范xml格式
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("src/xml/hero.xml"),OutputFormat.createPrettyPrint());
        xmlWriter.write(doc);
    }
data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

4. The case of writing the information in the object into the Xml document:

package work.one;

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

package work.one;

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

package work.one;

public class Monkey  extends Animal{
    
    
    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;
    }
}

package work.one;

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("day23-java23/src/work/one/animal.xml");
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter(out, format);
        writer.write(doc);
        writer.close();
    }
}

Insert picture description here

to sum up

The above is the entire content of the Xml document related methods and cases, mainly the content increase and query methods of Xml.

Guess you like

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