java——XML, XML parsing, design patterns

XML overview

  • XML is the abbreviation of Extensible Markup Language (eXtensible Markup Language), which is a data representation format that can describe very complex data structures and is often used to transmit and store data
  • XML usage scenarios: XML content is often used as a message for network transmission, or as a configuration file for storing system information

XML syntax rules

  • The suffix of the XML file is: xml
  • The document declaration must be the first line.
    insert image description here
    The label consists of a pair of angle brackets and a legal identifier: <name></name>, there must be one root label, and there can only be one.
    Tags must appear in pairs, with a start and an end: <name></name>
    special tags can be unpaired, but must have an end tag, such as: <br/>
    attributes can be defined in tags, attributes and tag names are separated by spaces, and attribute values ​​must be enclosed in quotation <student id = “1”></name>
    marks. correct nesting

Comment information can be defined in the XML file: <!- Comment content -->
The following special characters can exist in the XML file
insert image description here

A CDATA section can exist in an XML file: <![CDATA[ …content… ]]>

<?xml version="1.0" encoding="UTF-8" ?>
<student>
    <name>女儿国王</name>
    <sex></sex>
    <hobby>唐僧,追唐僧</hobby>
    <info>
        <age>30</age>
        <addr>女儿国</addr>
    </info>
    <sql>
        select * from user where age &lt; 18;
        select * from user where age &lt; 18 &amp;&amp; age &gt; 10;
        <![CDATA[
        select * from user where age < 18 && age > 10;
        ]]>
    </sql>
</student>

What is the composition format requirement of XML?
The file suffix must be the xml
document declaration must be the first line
There must be a root tag, and there can only be one
XML file that can define comment information: <!– comment content -->
tags must appear in pairs, with a start and a End tag:
must be able to nest correctly
What is a document constraint
Since XML files can customize tags, XML files can be defined at will, and problems may occur when the program parses Document
constraints: used to limit the tags and attributes in the xml file How should it be written
Requirements: Use DTD document constraints to constrain the writing of an XML file.
Analysis:
①: To write a DTD constraint document, the suffix must be .dtd
insert image description here

②: Import the DTD constraint document into the XML file that needs to be written
③: Write the content of the XML file according to the constraints.

//data.dtd文件
<!ELEMENT 书架 (书+)>
        <!ELEMENT  (书名,作者,售价)>
        <!ELEMENT 书名 (#PCDATA)>
        <!ELEMENT 作者 (#PCDATA)>
        <!ELEMENT 售价 (#PCDATA)>

//xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE 书架 SYSTEM "data.dtd">
<书架>
    <>
        <书名>女儿国王</书名>
        <作者></作者>
        <售价>唐僧,追唐僧</售价>
    </>
    <>
        <书名>人鬼情未了</书名>
        <作者>李四  </作者>
        <售价>唐僧,追唐僧</售价>
    </>


</书架>

document constraint-schema

  • Schema can constrain specific data types, which is more powerful in constraining ability
  • The schema itself is also an xml file, which is also required by other constraint files, so writing is more serious
  • Advantages of XML document constraints-schema
  • Can constrain the tag content format of the XML file, as well as specific data types

XML parsing

The role of XML data, how to deal with it in the end?

  • Store data, make configuration information, and perform data transmission.
  • Finally, it needs to be read by the program and parse the information inside.
    What is XML parsing?
  • Use the program to read the data in XML,
    two kinds of parsing
  • SAX parsing
  • DOM parsing (emphasis)
    insert image description here
<?xml version="1.0" encoding="UTF-8" ?>
<students>
    <!--第一个学生信息-->
    <student id="1">
        <name>张三</name>
        <age>23</age>
    </student>
    <!--第二个学生信息-->
    <student id="2">
        <name>李四</name>
        <age>24</age>
    </student>
</students>

insert image description here
insert image description here
insert image description here

<?xml version="1.0" encoding="UTF-8"?>
<contactList>
    <contact id="1" vip="true">
        <name>   潘金莲  </name>
        <gender></gender>
        <email>[email protected]</email>
    </contact>
    <contact id="2" vip="false">
        <name>武松</name>
        <gender></gender>
        <email>[email protected]</email>
    </contact>
    <contact id="3" vip="false">
        <name>武大狼</name>
        <gender></gender>
        <email>[email protected]</email>
    </contact>
    <user>
    </user>
</contactList>

package com.itheima.d1_dom4j;

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

import java.io.File;
import java.io.InputStream;

/**
 * 目标:学会使用dom4j解析XML文件中的数据
 * 1.导入dom4j框架
 * 2.准备一个xml文件
 */
public class Demo4JHelloWorldDemo1 {
    
    
    @Test
    public void parseXMLData() throws Exception {
    
    
        //1. 创建一个Dominatedj的系欸其对象,代表了整个代表了整个Dom4j框架
        SAXReader saxReader=new SAXReader();
        //2.把XML文件加载到内存中称为一个Domcument文档对象
        //注意:/是直接去src下寻找文件
        InputStream is=Demo4JHelloWorldDemo1.class.getResourceAsStream("/Contacts.xml");
        Document document=saxReader.read(is);
        //3.获取跟元素对象
        Element root=document.getRootElement();
        System.out.println(root.getName());

    }
}

Guess you like

Origin blog.csdn.net/weixin_46362658/article/details/123819058