XML ten minutes to get started quickly, Xiaobai must see

XML

This document is suitable for XML beginners' reference, the content is extremely simple and quick to get started

You can also take a look at other blogger articles, which are super suitable for beginners:

MySQL must know and know, delivery address: Use of MySQL, just read this article.
HTML & CSS must-see for beginners: HTML & CSS must know and know

1. Introduction to XML

XML is an extensible markup language

Second, the role of XML

  1. Used to save data, and these data are self-descriptive
  2. Can be used as a configuration file for a project or module
  3. It can be used as a format for network transmission data (less used, currently mainly JSON)

Three, the creation of XML

  1. Create a JAVA project or Moudle first, right-click the project and create a new Directory
  2. Right click on the folder new --> File
  3. Name the file xxx.xml

Fourth, the use of XML

  1. The annotation format of the XML file is the same as HTML:<!--注释内容-->
  2. The first line of each xml file must be: <?xml version="1.0" encoding="UTF-8"?>
    Note:
    ① There can be no spaces in <?xml, otherwise an error will be reported
    ② version represents the version number
    ③ encoding is the file encoding of XML
  3. The usage of XML tags and attributes is consistent with HTML
  4. Large paragraphs of text in XML can use the text area (CDATA area). The content in this area will not be parsed and will only be treated as plain text.
    Syntax:<![CDATA[输入的内容]]>

Code example:

<?xml version="1.0" encoding="utf-8" ?>
<books> <!--表示有多个book-->
    <book id="SN123"> <!--book标签描述一本书,id属性描述编号-->
        <!--以下标签描述图书的信息-->
        <name>时间简史</name>		
        <author>霍金</author>
        <price>23.6</price>
    </book>
    <book> <!--book标签用来描述另一本书-->
        <name>毛泽东语录</name>
        <author>毛泽东</author>
        <price>10.5</price>
    </book>
<![CDATA[<book>我不会被解析</book>]]>
</books>

Five, DOM4J analysis technology

  1. Both XML files and HTML files can be parsed using DOM technology. DOM uses XML documents as a tree structure. When using DOM4J, you need to download the jar package from the official website
  2. The use of DOM4J programming
    (1) Create a new libs folder under the current Moudle, import the above jar package, right click Add as Library
    (2) Create an XML file that needs to be parsed
<?xml version="1.0" encoding="UTF-8" ?>
<books>
    <book sn="SN123">
        <name>白马啸西风</name>
        <price>33.5</price>
        <author>金庸</author>
    </book>
    <book sn="SN456">
        <name>汇编语言</name>
        <price>25.9</price>
        <author>王爽</author>
    </book>
</books>

(3) Create the Book class corresponding to the XML file

public class Book {
    
    
    private String name;
    private String sn;
    private Double price;
    private String author;
    //无参、全参构造器
    public Book(String name, String sn, Double price, String author) {
    
    
        this.name = name;
        this.sn = sn;
        this.price = price;
        this.author = author;
    }
    public Book() {
    
    
    }
    //各属性的getter/setter方法
    public String getName() {
    
    
        return name;
    }
    public void setName(String name) {
    
    
        this.name = name;
    }
    public String getSn() {
    
    
        return sn;
    }
    public void setSn(String sn) {
    
    
        this.sn = sn;
    }
    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;
    }
    //toString方法
    @Override
    public String toString() {
    
    
        return "Book{" +
                "name='" + name + '\'' +
                ", sn='" + sn + '\'' +
                ", price=" + price +
                ", author='" + author + '\'' +
                '}';
    }
}

(4) Traverse the content of all tags in the XML file

public class Test {
    
    
    @org.junit.Test
    public void readXMl() throws DocumentException {
    
    
        //1.创建SAXReader对象通过read方法读取XML文件
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read("src/books.xml");
        //2.通过Document对象的getRootElement方法得到XML文件的根元素对象
        Element rootElement = document.getRootElement();
        //3.通过Element中的elements(标签名)或element(标签名)获取当前元素下
        //  的指定子元素集合(加s)或子元素(不加s)
        List<Element> books = rootElement.elements("book");
        //4.遍历每个book标签对象,获取到book对象内的每一个子元素
        for (Element book: books) {
    
    
            /***********************************************************/
            //获取到book下的name元素
            Element name = book.element("name");
            //Element的getText方法获取标签中的文本内容
            String nameText = name.getText();
            /***********************************************************/
            //获取到book下的sn属性,Element的attributeValue方法可以获取属性
            String snValue = book.attributeValue("sn");
            //获取到book下的price元素,elementText方法直接获取指定标签中的文本内容
            String priceText = book.elementText("price");
            //获取到book下的author元素的内容
            String authorText = book.elementText("author");
            //使用得到的标签内容创建Book对象,并输出
            System.out.println(new Book(nameText,snValue,
                    Double.parseDouble(priceText),authorText));
        }
    }
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49343190/article/details/107729256