XML introduction and use
XML is Extensive Markup Language, an extensible markup language. XML and html are very similar (I recommend to learn about html first). They both use writing tags and have an extension of .xml. At the same time, XML is very readable. Unlike htnl, there are no predefined tags for XML, while there are a lot of predefined tags for html. XML focuses on saving and transmitting data, while html is used to display information. The code comparison between xml and html is given below
html:
<body>
<!---<a>为预定义标签,具有超链接的功能--->
<a href="index.html">首页</a>
<body>
xml:
<school>
<class no="G3C1">
<grade>三年级</grade>
<class>一班</class>
</class>
</school>
The uses of XML are as follows:
- The configuration descriptor of the java program
-
Used to save the data generated by the program
- Data transmission between networks
XML document structure
- The first line must be an XML declaration
The XML declaration describes the basic information of the XML document, including the version and character set, written in the first line of the XML
- One and only one root node
- The writing rules of XML tags are the same as HTML
XML grammar rules
- Legal label name
Tag names should be meaningful. It is recommended to use English lowercase letters, separate words with "-", and it is recommended not to use duplicate names between multi-level tags (such as parent tags and word tags)
- Appropriate comments and indentation
Just for easy reading and maintenance
- Fair use of attributes
Multiple tags of the same type can use attributes
- Special characters and CDATA tags
In the tag body, "<",">" special characters appear, which will destroy the document structure, such as
Solution one entity reference:
The above xml can be written as:
Solution two use CDATA:
For a large number of special characters, you can use the CDATA tag. The writing format is "<![CDATA[text]]>", the text part xml will not be parsed, such as:
The <body> part and the paragraph above will not be parsed and output directly
- Ordered sub-elements
The tags used for the same sub-elements should be consistent for better readability
XML semantic constraints
The structure of the XML document is correct but not necessarily valid. For example, the "plant variety" tag cannot appear in the employee files. This is not semantically consistent. There are two ways to restrict XML semantics: DTD and XML Schema
- DTD Document Type Definition
DTD is a simple way of semantic constraint, usually appears as a separate file, and its extension is .dtd
The <!ELEMENT> tag defines the number and number of nodes allowed in the XML document, for example:
If you need to have multiple child nodes, add the corresponding descriptor after the child node:
Use the <!DOCTYPE> tag in XML to reference DTD files
XML Schema
xml schema is more complex and rigorous than DTD, and provides more functions (for example: data type, format limitation, data range, etc.). It is a W3C standard. XML grammar rules are more complicated, but the idea is very simple. See here for details