XML is used as a common data exchange format, which is more and more wide. At present, many system configuration files use XML format. The many profiles involved in this book are XML format, and the new JSP specification has also launched a JSP document with XML syntax, so we must learn and master Java Web. Development technology must have some basic XML language knowledge. Due to the versatility and wide application of XML, many places have to use XML knowledge in many places after readers, so the understanding and mastery of XML knowledge is already engaged in e-commerce websites and related systems. The basic knowledge must be mastered by the programmer.
F pointing to the prostitute: What is a configuration file?
The profile is a file that records the configuration information of the application. For example, when a program is to connect to a database system on the network, you need to specify the IP address of the database server, the server's network listening port number, the name, username, and password of the database, which can be treated as the configuration information of the application. Do not write this information in the source program, but written in a text file. When the application is to connect to the database system, read the required information from the text file. If the information of the connected database system has changed, you only need to modify the text file without modifying the source program, this text file is application Profile of the program. Traditional profiles are usually used in the following format:
# Db.properties
DBServer = 192.168.0.100
Port = 1443
Database = EMP
Username = zxx
PASSWORD = 123
The configuration file above this format can only represent some simple information, and it is difficult to express information with hierarchical and data items to be dynamically increased. For example, if you want to express "every province and governor in a country, each city and mayor of each city, each town of each city and the descendant name ", It is difficult to express using the above format. Profiles in XML format are easy to express this hierarchy, please refer to the explanation of the following sections of this chapter.
Document statement
In a complete XML document, you must include an XML document declaration that must be in the first line of the document. This statement indicates that the document is an XML document and which XML version is compiled. The simplest XML document declaration syntax is as follows:
XML Version = "1.0"?>
Between "" and "?", "?" And ">" and the first "?" And XML can't have spaces, this is an embodiment of XML syntax strictness. In the second "?", There is no or one or more spaces. The version of the Version property in the statement represents the version of the XML, as the parser will definitely vary from the resolution of different versions, although only version 1.0 is currently, but must specify the Version property in the statement.
Since people can use different character coding sets to write a character content exactly the same XML document, the XML software tool (including the analyzer) needs to know the character encoding used by the XML document. This can be explained by specifying an encoding property in an XML document declaration, for example, using the following statement to indicate the character encoding mode in the document is GB2312 encoding.
XML Version = "1.0" encoding = "GB2312"?>
In the XML1.0 specification of W3C, all XML parsers must accept UTF-8 and UTF-16 encoded Unicode characters, so the software tools that meet the XML specification must support both Unicode encodes. If an encoding property is set in the XML declaration to explicitly specify the character encoding method used by the document, it will be treated in Unicode. The XML parser can automatically detect Unicode encoding in the document UTF8 by looking for byte sequential marks at the beginning of the XML document. That is, the default setting of the Encoding property is Unicode encoding. If the character in the document is used as UTF-8 or UTF-16 as encoding, this property may not be set. In the XML document declaration statement, you can also specify another optional attribute: Standalone, for example:
XML Version = "1.0" Encoding = "UTF-8" Standalone = "YES"?>
The Standalone property is used to explain whether the document is independent. If it is set to "Yes", it means that the document does not depend on any files that can be dependent on the outside, that is, it does not require the DTD file to verify whether the identity is effective, nor Unex, CSS files are required to control their display appearance; when the Standalone property is set to "NO", the document depends on a file outside, for example, depending on a DTD file or XSL, CSS file. The default value of the Standalone property is "YES".
Note: If the Encoding and Standalone properties are set, the Standalone property is located after the Encoding property.
: Hand experience: deepen understanding of Encoding properties
For XML containing Chinese characters, the characters may be encoded or GB2312 (Simplified Chinese character encoding) encoding, if the character in the document is encoded, the Encoding property is GB2312, by one Experiments to illustrate this problem.
(1) Create a file named book.xml with a notebook program with Windows, and the contents of the file are shown in routine 1-1. Routines 1-1 book.xml
XML Version = "1.0"?>
张孝祥 author>
Book>
张孝祥 author>
Book>
Bookshelf>
(2) Open the book.xml file with the browser above IE5.0, see the result is shown in Figure 1.2. The error prompt information is "Intaordable characters" in text content ", which is because the browser will use the default Unicode encoding to resolve the document with the default Unicode encoding in the Book.xml XML document declaration statement. And the characters in this document actually use GB2312 encoding, not Unicode encodes.
(3) Click the "View" "View" menu of the browser, modify the first line of the open book.xml file content into the following form: XML Version = "1.0" encoding = "GB2312"? > After saving modifications, refresh the browser window to display the Book.xml file, see the result is shown in Figure 1.3. Click the minus sign (-) before a tag (-), and all the contents nested in the tag will be folded, the minus sign (-) in front of the tag will also become a plus sign ( ). Click the plus sign ( ) in front of a tag, nesting all the content in this tag will be expanded, the plus sign ( ) in front of the tag will also become minus (-). (4) In the Notepad of the book.xml file above, click the "File" à "Save As" menu, in the Open Save As dialog box, select "Save Type" as "All Files", Encoded as "UTF-8", as shown in Figure 1.4. After saving the book.xml file with UTF-8, although there is no change in the record in the Notepad program window, the data stored inside the book.xml file has changed, which is based on the file size before and after the comparison Book.xml saved. It can be seen. Refresh the browser window to display the Book.xml file, see the result is shown in Figure 1.5. Because the current book.xml file has been converted to UTF-8, the eNCoding property is specified in the initial declaration of the document to be GB2312, so the browser will errors when parsing the Book.xml file.
(5) Modify the Encoding property of the Book.xml's document to UTF-8. After saving, refresh the browser window to display the book.xml file, you can see the normal display of Figure 1.3.