For the moment, use NotePad to create an XML file. First look at an XML file:
Example 1
<〈? XML version = "1.0" encoding = "gb2312 "? > 〉
<References> 〉
<Books> 〉
<Name> XML Quick Start </Name> 〉
<Author> Zhang San </author> 〉
<Price currency unit = "RMB"> 20.00 </price> 〉
</Books> 〉
<Books> 〉
<Name> XML syntax </Name> 〉
<〈! -- This book will be published soon --> --〉
<Author> Li Si </author> 〉
<Price currency unit = "RMB"> 18.00 </price> 〉
</Books> 〉
</References> 〉
This is a typical XML file. After editing, it is saved as a file suffixed with. xml. This file can be divided into two major parts: The PROLOG and the Object Body. The first line in this file is the preface of the file. This row is an XML file that must be declared and must be located in the first line of the XML file. It mainly tells the XML Parser how to work. The version indicates the standard version number used by the XML file, which must be available. The encoding indicates the character type used in the XML file, which can be omitted, when you omit this statement, the subsequent encoding code must be a unicode encoding code (we recommend that you do not omit it ). In this example, the gb2312 encoding code is used, so the encoding statement cannot be omitted. There are some statement statements in the preface of the file, which we will introduce later.
The rest of the file is the file subject, and the content of the XML file is stored here. We can see that the file body is composed of the starting reference and ending reference control mark, which is called the "root element" of the XML file "; books are sub-elements directly under the root element. Books have sub-elements such as name, author, and price. The currency unit is an "attribute" in the "price" element, and "RMB" is "attribute value ".
<〈! -- This book will be published soon --> like HTML, It is a comment. In the XML file, the comment is placed in "<! The part between the -- mark and the --> mark.
As you can see, the XML file is quite simple. Like HTML, XML files are also composed of a series of tags. However, tags in XML files are custom tags with clear meanings, we can describe the meaning of the content in the tag. After getting a preliminary impression on the XML file, let's talk about the syntax of the XML file in detail. Before speaking the syntax, we must understand an important concept: XML Parse ).
1. XML Parser
The main function of the parser is to check whether the XML file has a structure error, strip the mark from the XML file, read the correct content, and hand it to the next application for processing. XML is a markup language for structured file information. The XML specification provides a detailed rule for how to mark the structure of a file, the parser is the software written according to these rules (written in Java ). Like HTML, the browser must have an HTML Parser so that the browser can "read" various webpages consisting of HTML tags and display them in front of us. If the HTML Parser of the browser does not understand the mark, the error message will be returned to us.
Because the current HTML Tag is actually quite messy, there are a lot of nonstandard tags (some web pages can be normally displayed with IE, but not with Netscape Navigator), so from the very beginning, the XML designer strictly defines the XML syntax and structure, and the XML file we write must follow these rules, otherwise the XML parser will show you error messages without mercy.
There are two types of XML files: well-formed XML files and validating XML files.
If an XML file meets some relevant rules in the XML specification and does not use DTD (file format definition-later description), this file can be called well-formed. If an XML file is well-formed and the DTD is used correctly, and the syntax in the DTD is correct, the file is validating. There are two types of XML files, one is the well-formed parser and the other is the validating parser. IE 5 contains the validating parser, which can also be used to parse the well-formed XML file.
Check whether it meets the well-formed condition. You can open the first XML file you just edited in an IE 5 or later browser.
You may ask why the display in the browser is the same as that in my source file? That's right, because for XML files, we just need to render the contents, and its display form is handed over to CSS or XSL. Here, we have not defined its CSS or XSL file for this XML file, so it is displayed in the original format. In fact, for electronic data exchange, you only need an XML file. If you want to display it in some form, we must edit the CSS or XSL file (this issue will be discussed later ).
2. xml file of well-formed
We know that XML must be well-formed to be correctly parsed by the parser and displayed in the browser. So what is the XML file of well-formed? There are several principles below. We must satisfy them when creating XML files.
First, the first line of the XML file must be to declare that the file is an XML file and the XML standard version it uses. There cannot be other elements or comments before the file.
Second, there is only one root element in the XML file. In our first example, <references>... </References> is the root element of the XML file.
Third, the tag in the XML file must be properly closed, that is, in the XML file, the control tag must have a corresponding end tag. For example, the <Name> tag must have a corresponding </Name> end tag. Unlike HTML, the end tag of some tags can be unavailable. If you encounter a tag of a self-contained unit in an XML file, it is similar to when there is no ending mark, XML calls it "null element", which must be written as follows: <empty element name/> /〉, if an element contains an attribute, the write rule is: <empty element name property name = "attribute value"/> "/〉.
Fourth, do not cross tags. In the previous HTML file, you can write as follows:
<B>
There are overlapping areas between tags, and in XML, the format of such a tag is strictly prohibited. tags must appear in regular order.
Fifth, the attribute value must be included with the "" sign. For example, "1.0", "gb2312", and "RMB" in the first example ". They are all enclosed by "" and cannot be missed.
Sixth, the control tag, command, and attribute name must be case sensitive. Unlike HTML, in HTML, a tag similar to <B> and <B> has the same meaning. in XML, tags like <Name>, <Name>, or <Name> are different.
7. We know that in the HTML file, if we want the browser to display the entered content intact, you can place these items in the middle of the <PRE> </PRE> or <XMP> </XMP> mark. This is essential for creating html Teaching Web pages, because the source code of HTML must be displayed on the web pages. To implement such a function in XML, you must use the CDATA tag. The information in the CDATA tag is transmitted to the application by the parser without parsing any control flag in the segment. The CDATA region is composed of: "<! [CDATA ["indicates the Start mark, and"> "indicates the end mark. For example, the source code in example 2, except "<! [CDATA ["and"> ", the rest of the content parser will be left intact to the downstream application, even if the beginning and end of the CDATA area are blank and line breaks, are also transferred (note that CDATA is an uppercase character ).
Example 2
<〈! [CDATA [flying XML >>>> 〉〉〉〉〉,:-)
Oooo <<<<<<<< 〈〈〈〈〈〈〈
> 〉
Eighth, XML Processing of white space characters is different from HTML. According to the HTML standard, no matter how many white spaces there are, they are treated as a white space. In XML, all white spaces other than the mark are specified, the parser must be faithfully handed over to downstream applications for processing. In this way, we sometimes have to discard the habit of shrinking HTML files because the parser also needs to process the space. For example:
<Author> Zhang San </author> 〉
And
<Author> 〉
Zhang San
</Author> 〉
The above content is different for the Parser (except for the three characters in the latter, it also includes two line breaks and the text escape characters before "Zhang San ). Therefore, the parser will have different processing results after passing the information to the application after removing the mark.
If we want to clearly tell the XML program that the blank in the mark has a clear meaning, do not remove it (for example, in some poems, spaces have their specific meanings ), you can add an XML built-in Attribute-xml: space in the tag. For example (note the case sensitivity of attribute names and values ):
<Poetry XML: Space = "preserver"> "〉
Motherland! Motherland!
My motherland!
</Poetry> 〉
In addition, if you want to use special characters in table 1 in an XML file, you must use the corresponding symbol instead.
Table 1
Special Character replacement symbols
&&&
<& It;
>
""
'& Apos;
Here is a summary: the XML file that complies with the above rules is the XML file of well-formed. This is the most basic requirement for compiling XML files. The syntax of the XML file is much stricter than that of the HTML file. Due to such strict regulations, it is much easier for software engineers to write XML parsers. Unlike the HTML parsers, they must try their best to adapt to different web pages, improve the adaptability of your browser. In fact, this is also a good thing for beginners. You don't have to worry about the way HTML is written.
We can see that most of the XML files use custom tags. However, if two companies A and B in the same industry use XML files to exchange data with each other, Company A uses the price tag to indicate the price information of their products, company B may use the price to indicate the price information. If an XML application reads the information in their respective XML files, if it only knows the price information in the <price> mark, if the price information of Company B cannot be obtained, errors will occur. Obviously, for entities that want to use XML files to exchange information, there must be a convention between them-the tags that can be used to write XML files and the child elements that can be included in the parent element, the order in which each element appears, and how the attributes in the element are defined. In this way, they can communicate smoothly only when using XML to exchange data. This Convention is called DTD (Document Type Definition, document format definition ). You can regard DTD as a template for compiling XML files. For XML data exchange between the same industry, it is much easier to have a fixed DTD. For example, if the XML Web pages of major e-stores on the Internet follow the same DTD, we can easily compile an application based on this DTD, go online and automatically capture what we are interested in. In fact, there are already several defined DTD, such as MathML and SMIL mentioned above.
If an XML file is well-formed and is correctly created based on a DTD, the XML file is called validating XML. The corresponding parser is called validating parser.