I do not pay enough attention to XML and have a very superficial understanding, because in my previous Web experience, JSON can basically be used to replace XML. JSON has less network traffic and fast resolution, I have a strong point of view on my own thanks to JS support. However, I gradually changed these points of view. Recently I wrote a Spring-based service configuration system. The main idea is to encapsulate some common services into service classes and reserve some parameter interfaces, then, these parameters and various relationships between service classes are injected through Spring to implement business logic configuration and code reuse and maintenance as much as possible. during actual development and application, the Spring configuration file is getting bigger and bigger, which leads to the idea of separating service configuration from individual management, however, this idea suffered a huge setback from the very beginning-because of my ignorance in the XML field. So I thought it would be better to take this opportunity to complete the XML knowledge!
1. XML Overview
(1) The full name of XML is Extensible Markup Language, which can be used to expand the Markup Language. In terms of name, XML is a Markup Language, which is relatively simple, unlike programming languages such as C and Java, the Markup Language XML is extensible. Unlike HTML (Hypertext Markup Language, Hypertext Markup Language), it has a large number of built-in labels, HTML does not have scalability. Data logic and presentation logic are mixed together. XML can be expanded freely and is only responsible for data storage, regardless of how the data is displayed.
(2) XML can be divided into three types:
A: malformed: this is not an XML document because it does not comply with the most basic XML document rules.
B: well-formed: complies with the basic rules of the XML document, but does not use semantic constraints such as DTD or Schema, however, the document does not fully comply with the corresponding semantic constraints.
C: valid XML document (valid): complies with the basic rules of XML documents, and uses DTD or Schema semantic constraints while fully complying with these semantic constraints.
Here, the classification of XML documents is based on "Basic Rules of XML documents". What are the basic rules?
(3) basic rules for XML documents
A: There is only one root element.
B: The element must have an end label.
C: The elements must be correctly nested. do not include me. I have you in it. You cannot tell who is the master or who is the master.
D: the attribute of an element must have a value, which is enclosed by single or double quotation marks. That is to say, the attribute of an element must be enclosed by single or double quotation marks. As for the content in the quotation marks, no matter, the order between multiple attributes is random.
XML documents that comply with the basic rules can be converted into a tree structure.
Of course, the basic rule questions in XML documents should include the element names and attribute names which are also valid identifiers.
2. XML Declaration
We can often see that the first line of an XML document is often similar to the following text, which is called an XML declaration.
(1) XML declaration with <? Start with xml, with?> End.
(2) The version attribute in the XML declaration is required, usually 1.0, indicating that this document complies with the XML 1.0 standard.
(3) Character Set attribute encoding is optional, default value is UTF-8.
(4) The standlone attribute is also optional, indicating whether to reference other resources. The value can only be yes or no.
3. Tag Name
(1) tag names are case sensitive. name and Name indicate two tag names.
(2) A Tag name may consist of letters (Chinese characters), numbers, underscores (_), hyphens (-), colons (-), and periods (-), but cannot begin with a number, hyphen (-), or dot.
(3) The tag name cannot contain special characters such as <,>, $, and space. It cannot begin with xml (or any combination of xml sizes ).
(4) do not use any colon unless the namespace is used.
The label name is similar to the variable name in other languages. You do not need to worry too much about it, and generally do not encounter errors unless you try to verify the correctness of the XML tool.
4. entity reference
In XML documents, because tags are enclosed by Angle brackets, angle brackets have special meanings in the content of XML documents, if you don't want to bother, you 'd better use entity references to represent these special symbols (similar to escaping in other languages, escape the meaning of the special symbols as the characters themselves ), there are five built-in object references. Of course, you can also Customize Object references-this is the last thing:
Entity reference |
Symbol |
Description |
& Lt; |
< |
Less than symbol |
& Gt; |
> |
Greater than symbol |
& Amp; |
& |
And symbol |
& Apos; |
' |
Single quotes |
& Quot; |
" |
Double quotation marks |
Are you familiar with this format? Similar to HTML, it starts with "&" and ends with ";", except that HTML has more built-in entity references.
If the TAG content has many entity references, it will be very unsightly. in XML, you can also use the CDATA tag to mark a whole segment of content, make the content completely loyal to the symbol itself. The format is as follows:
Let's look at an example:
You have seen the <! --... --> Indicates a comment. Do not:
(1) do not place it inside the tag. Otherwise, it will destroy the structure of the XML document.
(2) do not place comments before the XML declaration. The XML declaration will always be placed in the first line of the document.
(3) do not use -- in the middle of comments. This will be confusing. Naturally, the comments should not end with -->.
5. processing commands
XML also provides a syntax called processing commands to tell the processing program how to process them. developers can define arbitrary processing commands and associate appropriate programs to parse the commands.
The XML Processing Instruction format is:
In terms of form, we can regard the XML Declaration as a processing instruction named xml. Another common Processing Instruction is xml-stylesheet, which is used to import style sheets (such as css style sheets and xsl style sheets) to XML documents ). For example: