xml语法及其封装成类

xml文件





<books>
    <book sn="01">
        <name>时间简史name>
        <author>霍金author>
        <price>666price>
    book>
    <book sn="02">
        <name>时间捡屎name>
        <author>
            
            
            >>]]>
        author>
        <price>666price>
    book>
    
    

books>

提取xml信息类

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import java.util.List;

public class Dom4jTest {
    @Test
    public void test1() throws DocumentException {
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read("src/books.xml");
            System.out.println(document);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //    读取xml文件生成book类
    @Test
    public void test2() throws DocumentException {
        // 读取books.xml文件
        SAXReader reader = new SAXReader();
        // 在junit测试中,相对路径是从模块名开始算的
        Document document = reader.read("src/books.xml");
        // 通过Document对象获取根元素
        Element rootElement = document.getRootElement();
//        System.out.println(rootElement);
        List<Element> books = rootElement.elements("book");
        // 遍历
        for (Element book : books) {
//            System.out.println(book.asXML());
            Element nameElement = book.element("name");
            System.out.println(nameElement.asXML());
            String name = nameElement.getText();
            System.out.println(name);
            String price = book.elementText("price");
            System.out.println(price);
            String author = book.elementText("author");
            System.out.println(author);
            String snValue = book.attributeValue("sn");
            // 将取出的元素进行封装
            System.out.println(new book(snValue, name, Double.parseDouble(price), author));
        }
    }
}

封装类

import java.math.BigDecimal;

public class book {
    private String sn;
    private String name;
    private double prices;
    private String 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 getPrices() {
        return prices;
    }

    public void setPrices(double prices) {
        this.prices = prices;
    }

    public String getAuthor() {
        return author;
    }

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

    public book(String sn, String name, double prices, String author) {
        this.sn = sn;
        this.name = name;
        this.prices = prices;
        this.author = author;
    }

    public book() {

    }

    @java.lang.Override
    public java.lang.String toString() {
        return "book{" +
                "sn='" + sn + '\'' +
                ", name='" + name + '\'' +
                ", prices=" + prices +
                ", author='" + author + '\'' +
                '}';
    }
}

你可能感兴趣的:(javaweb笔记,xml,封装)