使用dom4j解析xml文件

28 篇文章 0 订阅

创建一个图书的book.xml文件

<?xml version="1.0" encoding="utf-8" ?>

<books>
    <book sn="SN1232131511535">
        <name>时间简史</name>
        <author>霍金</author>
        <price>75</price>
    </book>
    <book sn="SN1232131511323">
        <name>java</name>
        <author>张三</author>
        <price>6.6</price>
    </book>
</books>

根据XML文件创建对应的Book

public class Book {
    
    private String sn;
    private String name;
    private float price;
    private String author;

    public Book() {
    }

    public Book(String sn, String name, float 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 float getPrice() {
        return price;
    }

    public void setPrice(float 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 + '\'' +
                '}';
    }
}

使用dom4j读取xml文件。

	@Test
    public void test1() throws Exception {

        //创建一个saxReader输入流,去读取xml配置文件,生成Document对象
        SAXReader saxReader = new SAXReader();

        Document document = saxReader.read("src/book.xml");

  		System.out.println(document);
        
    }

把XML文件中的<book></book>都解析成对应的Book类的对象实例

	@Test
    public void test2() throws DocumentException {
        //1.读取book.xml文件
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("src/book.xml");

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

        //3.通过根元素获取book标签对象
        List<Element> books = rootElement.elements("book");

        //4.遍历,处理每个book标签转换成Book类
        for (Element book : books){
            //asXML()把标签对象,转换为标签字符串
            Element nameElement = book.element("name");
            
            //getText()可以获取标签中的文本内容
            String nameText = nameElement.getText();
            //直接获取指定标签名的文本内容
            String priceText = book.elementText("price");
            String authorText = book.elementText("author");

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

            System.out.println(new Book(snValue, nameText, Float.parseFloat(priceText), authorText));

        }
    }

输出结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值