1. Introduction to XML
XML is an extensible markup language.
XML tags are not predefined, that is, they are not fixed. Users can define tags according to their own needs.
XML is mainly used to store and transmit data
Stored data is used to store hierarchical relationships, such as provinces and cities. In the java field, XML is mainly used as a framework configuration file
1.spring framework application.xml
2.mybatis mybatis.xml
3.log4j2 log4j2.xml
Transmission data refers to the data transmission format in which the service provider returns a service caller. At present, the mainstream data transmission format is json.
The current version of XML is 1.0
2. Element composition of XML
What can I write when writing an XML file?
- Document declaration
<?xml version="1.0" encoding="UTF-8" ?>
The document declaration of XML is optional, that is, it can not be written, but it will be written for Standardization in development.
If the XML document declaration is written, it must be placed in the first row and first column of the XML file, and must be marked with <? XML, and? > And must contain two attributes: version and encoding.
1.version indicates the version number of XML
2.encoding means XML encoding, usually written in UTF-8
- Element (label)
Elements are an important part of XML, and elements are also called tags.
Each XML file must have a root tag.
The tag consists of a start tag and an end tag. You can write a tag or a text string in the middle.
Tags can be nested, and the nesting relationship of the hierarchy must be clear
Label names must comply with naming rules and specifications
1. The tag name cannot start with a number
2. Spaces and colons cannot be used
3. XML cannot be used
4. Naming is case sensitive
<?xml version="1.0" encoding="UTF-8" ?> <users> <user> <id>1001</id> <name>admin</name> <password>12345</password> </user> <user> <id>1002</id> <name>jack</name> <password>6666</password> </user> </users>
- attribute
The attribute is a part of the tag (element), and the attribute can only be defined in the start tag,
Attribute definition format. Attribute name = attribute value. Attribute value needs to be enclosed in double quotation marks.
Multiple attributes can be defined in the start tag, but the attribute names of multiple attributes cannot be the same.
Like tag names, attribute names should also follow naming rules and specifications.
<?xml version="1.0" encoding="UTF-8" ?> <users> <user id="1001" country="china" source="android"> <id>1001</id> <name>admin</name> <password>12345</password> </user> <user id="1002" country="china" source="iso"> <id>1002</id> <name>jack</name> <password>6666</password> </user> </users>
- annotation
Comments are descriptions of xml tags
Shortcut keys: Ctrl+/
<!--Comment pair xml Description of the document-->
-
Escape character
Some characters in xml are unrecognizable, so we need to use the escape character of xml to represent them.
For example: & Use & amp; To represent
< use & lt; To represent -
Character area
All contents in CDATA will be parsed as text, and the character area will be used to solve the problem that some characters in xml cannot be recognized.
The shortcut key in CDATA in IDEA is CD + enter.
<?xml version="1.0" encoding="UTF-8" ?> <!--Comment pair xml Description of the document--> <users> <user id="1001" country="china" source="android"> <id>1001</id> <name>admin</name> <password>12345</password> <salary>salary>500 & salary < 3000</salary> </user> <user id="1002" country="china" source="iso"> <id>1002</id> <name>jack</name> <password>6666</password> <salary><![CDATA[ salary> 500 & salary < 3000 ]]></salary> </user> </users>
3. Introduction to XML constraints
Because xml tags are not predefined, you can add any tags when writing xml files, which is not conducive to parsing.
In the technical example of xml, you can write an xml constraint file to constrain the specification written in xml, which is called xml constraint.
There are two types of xml constraints:
1.DTD constraint (the file suffix is. DTD) the web.xml of java web project uses DTD constraint.
2.Schema constraint (the suffix of the file is. xsd). The application.xml of spring framework uses schema constraint.
Schema constraints are more powerful than DTD constraints.
1. The type of data is more perfect
2. More flexible restrictions on the number of occurrences of elements
In daily development, we usually don't write constraint files, but we need to write xml files that meet the specifications according to the constraint files, so we can understand them.
xml constraints constrain the contents of xml files
1. Restrict the hierarchical relationship of xml tags (root tag, sub tag, sub tag)
2. Constraint label (name, type)
3. Attribute of constraint label (attribute name, attribute type, attribute constraint)
4.XML constraint - DTD constraint
First, learn about quantifiers
- +Indicates that 1-n elements can appear
- *Indicates that 0-N can appear
- , indicating that the elements must appear in the specified order
- |Represents one of the enumerations
1. Hierarchical relationship of constraint labels
<!ELEMENT label (Sub label)>
<?xml version="1.0" encoding="UTF-8" ?> <!ELEMENT users (user+)> <!ELEMENT user (id,name,password)>
2. Constraint label
<!ELEMENT Tag name (Label type)>
Type of label:
- EMPTY empty type
- ANY any type
- #PCDATA string
<!ELEMENT id (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT password (#PCDATA)>
3. Attribute of constraint label
<!ATTLIST Tag name Property name property type property constraint Property name property type property constraint Property name property type property constraint Property name property type property constraint >
Attribute type
- CDATA: represents a text string
- ID: represents a unique in the xml file and cannot start with a number
- ENUMERATED: indicates that the type of the property is enumeration. You can only select one from the list of enumerations
Attribute constraint
- REQUIRED: indicates that the attribute must exist
- Implied: indicates that the attribute is optional
- FIXED: indicates that the attribute has a FIXED value. The format is #FIXED "FIXED value"
<!ATTLIST user id ID #REQUIRED country CDATA #IMPLIED platform CDATA #FIXED "ittimeline" source(android|iso|wechat) "android" >
Use of DTD constraints
According to the location of DTD constraint files, there are three ways to use them
1. External DTD - local DTD. The DTD document is in the local disk path, but it is not in the same file as the xml file. The DTD document and the xml file are usually in the same directory.
<!DOCTYPE Root element SYSTEM 'DTD Constraint file name'>
<!DOCTYPE users SYSTEM 'user.dtd'>
2. External DTD - public DTD. DTD files are generally provided by the framework on the network, which is also the most used by us.
<!DOCTYPE Root element PUBLIC 'DTD Constraint file name' "DTD Document URL">
3. Internal DTD, XML file and DTD document in one XML file, DTD constraint will only be valid for the current XML file
<!DOCTYPE Root element [Element constraint declaration]>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE users [ <!ELEMENT users (user+)> <!ELEMENT user (id,name,password)> <!ELEMENT id (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT password (#PCDATA)> <!ATTLIST user id ID #REQUIRED country CDATA #IMPLIED platform CDATA #FIXED "ittimeline" source (android|iso|wechat) "android" > <!-- There must be spaces!!!!!!!--> ]> <!--Comment pair xml Description of the document--> <users> <user id="s1001" country="china" source="android"> <id>1001</id> <name>admin</name> <password>12345</password> <!-- <salary>salary>500 & salary < 3000</salary>--> </user> <user id="s1002" country="china" source="iso"> <id>1002</id> <name>jack</name> <password>6666</password> <!-- <salary><![CDATA[--> <!-- salary> 500 & salary < 3000--> <!-- ]]></salary>--> </user> </users>
5.XML constraint Schema constraint
Definition of Schema constraints
<?xml version="1.0" encoding="UTF-8" ?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.ittimeline.net" elementFormDefault="qualified" > <!-- Standard namespace, fixed writing--> <xs:element name="orders"> <xs:complexType> <!-- Defining complex labels can contain multiple child labels--> <xs:sequence maxOccurs="3" minOccurs="1"> <!-- There are at most several sub tags and at least several sub tags--> <xs:element name="order"> <xs:complexType> <xs:sequence> <!-- Sort by the contents of child label elements--> <xs:element name="id" type="xs:unsignedInt"></xs:element> <xs:element name="address" type="xs:string"></xs:element> <xs:element name="bookTime" type="xs:dateTime"></xs:element> <xs:element name="amount" type="xs:double"></xs:element> </xs:sequence> <!--Define properties--> <xs:attribute name="orderId" type="xs:unsignedInt" use="required"></xs:attribute> </xs:complexType> </xs:element> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
6. Use of schema constraints
<?xml version="1.0" encoding="UTF-8" ?> <orders xmlns="https://www.ittimeline.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.ittimeline.net order.xsd"> <order orderId="1001"> <id>1001</id> <address>Huangpu District, Shanghai</address> <bookTime>2021-06-20T08:00:00</bookTime> <amount>8888.88</amount> </order> <order orderId="1002"> <id>1002</id> <address>Shanghai changmingqiao</address> <bookTime>2021-06-20T08:00:00</bookTime> <amount>8888.88</amount> </order> <order orderId="1003"> <id>1003</id> <address>Jing'an District, Shanghai</address> <bookTime>2021-06-20T08:00:00</bookTime> <amount>8888.88</amount> </order> </orders>