xml-使用dom4j读取xml文件得到Document对象、使用dom4j解析xml
package top.qaqaq.java;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.jupiter.api.Test;

import java.util.List;

/**
 * @author RichieZhang
 * @create 2022-12-04 下午 1:45
 */
public class Dom4jTest {

    @Test
    public void test1() throws Exception {
        // 创建一个SaxReader输入流,去读取xml配置文件,生成Document对象
        SAXReader reader = new SAXReader();

        Document document = reader.read("src/books.xml");

        System.out.println(document);
    }

    /**
     * 读取books.xm文件生成Book类
     */
    @Test
    public void test2() throws Exception {
        //1. 读取books.xml文件
        SAXReader reader = new SAXReader();
        // 在Junit测试中,相对路径是从模块名开始算
        Document document = reader.read("src/books.xml");

        //2. 通过Document对象获取根元素
        Element rootElement = document.getRootElement();
        System.out.println(rootElement);

        //3. 通过根元素获取book标签对象
        // element()和elements() 都是通过标签名查找子元素
        List<Element> books = rootElement.elements("book");

        //4. 遍历,处理每个book标签转换为book类
        for (Element book: books) {
            //asXml() 把标签对象。转换为标签字符串
            Element nameElement = book.element("name");
            System.out.println(nameElement.asXML());

            // getText() 可以获取标签中的文本内容
            String nameText = nameElement.getText();
            System.out.println(nameText);

            // 直接 获取指定标签名的文本内容
            String priceText = book.elementText("price");
            System.out.println(priceText);

            String authorText = book.elementText("author");
            System.out.println(authorText);

            String snValue = book.attributeValue("sn");

            System.out.println(new Book(snValue,nameText,Double.parseDouble(priceText),authorText));

        }

    }
}
package top.qaqaq.java;

/**
 * @author RichieZhang
 * @create 2022-12-04 下午 1:36
 */
public class Book {

    private String sn;
    private String name;
    private Double price;
    private String author;

    public Book() {
    }

    public Book(String sn, String name, Double price, String author) {
        this.sn = sn;
        this.name = name;
        this.price = price;
        this.author = author;
    }

    public String getSn() {
        return sn;
    }

    public void setSn(String sn) {
        this.sn = sn;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    @Override
    public String toString() {
        return "Book{" +
                "sn='" + sn + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}

books.xml

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book sn="SN12341232">
        <name>辟邪剑谱</name>
        <price>9.9</price>
        <author>班主任</author>
    </book>
    <book sn="SN12341231">
        <name>葵花宝典</name>
        <price>99.99</price>
        <author>班长</author>
    </book>
</books>
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇