Java Web(3)-XML

1、 Introduction to XML

1. What is XML?

XML is an extensible markup language

2. What is the role of XML?

2、 XML syntax

First, create an XML file

<?xml version="1.0" encoding="UTF-8" ?>
<!--
    文件的声明
    version="1.0"        表示xml的版本
    encoding="UTF-8"     表示xml文件本身的编码
-->

Books have logo, title, author and price information

<?xml version="1.0" encoding="UTF-8" ?>
<!--
    文件的声明
    version="1.0"        表示xml的版本
    encoding="UTF-8"     表示xml文件本身的编码
-->

<!-- 多个图书信息-->
<books>

    <!--一个图书的基本信息-->
    <book sn="SN123454321">    <!--属性值-->
        <name>雍正王朝</name>
        <author>二月河</author>
        <price>99</price>
    </book>

    <book sn="SN987654321">
        <name>康熙大帝</name>
        <author>二月河</author>
        <price>99</price>
    </book>
</books>

be careful:

3、 XML parsing

Here we mainly use Dom4j technology to parse

First, you need to find the jar package on the official website and directly copy it to the current module

1. Dom4j programming steps

2. Get the document object

1. First create a lib package, directly copy the jar package of Dom4j found on the Internet to lib, and then

2. Write a book that needs to be parsed XML file

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

<books>
    <book sn="SN123454321">
        <name>雍正王朝</name>
        <author>二月河</author>
        <price>99</price>
    </book>

    <book sn="SN987654321">
        <name>康熙大帝</name>
        <author>二月河</author>
        <price>99</price>
    </book>
</books>

3. Get the code of document object

@Test
    public void test1() throws DocumentException {

        // 1. 先创建一个SAXReader对象
        SAXReader saxReader = new SAXReader();
		// 2. 这个对象用来读取xml文件,返回一个document
        Document read = saxReader.read("src/books.xml");
		// 3. 打印到控制台,查看是否成功
        System.out.println(read);

    }

3. Traverse the tag to get the content

There are four steps:

First, write a Book class to store data

package com.md.java;
/**
 * @author MD
 * @create 2020-07-24 8:45
 */
public class Book {
    private String sn;
    private String name;
    private String author;
    private Double price;

    public Book() {
    }

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

    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 String getAuthor() {
        return author;
    }

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

    public Double getPrice() {
        return price;
    }

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

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

Read the contents of the XML file. The XML file is the books xml

    /**
     * 读取books.xml文件生成的Book类
     */
    @Test
    public void test2() throws DocumentException {
        // 1. 读取文件,在Junit测试中,相对路径是从模块名开始的
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.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()把标签对象转换为标签字符串
            // System.out.println(book.asXML());

            // 5. 获取到了这个标签
            Element nameElement = book.element("name");
            // 获取这个标签的文本内容
            String name = nameElement.getText();

            // 或者可以直接获取标签里的文本内容
            String price = book.elementText("price");
            String author = book.elementText("author");

            // 获取属性值
            String sn = book.attributeValue("sn");


            // 获取完成之后就可以生成了,注意价格是double类型的
            System.out.println(new Book(sn,name,author,Double.parseDouble(price)));

        }

    }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>