The same day ago, I was discussing with my colleagues the relationship between the encoding attribute in xml and the file format, and finally understood it thoroughly.
It was previously understood that the encoding definition in xml must match the file format. That is, if there is such an XML Introduction . (I later found out that FF FE is not the BOM of utf-8... which means that my misunderstanding lasted for quite a while...)
Let's briefly talk about the several stages of the discussion.
At the beginning of the discussion, I told him for sure that the encoding value must match the file format (ie BOM, BOM is the abbreviation of byte order mark), otherwise when parsing XML, errors may occur (for example, the document contains A certain UNICODE character, and the format specified by encoding or BOM does not match, an error will occur. This is what I meant at the time), and then he told me that it seemed not to be the case. The XML file I created with DELPHI did not have a BOM in the XML. There is Chinese content, and the encoding specified is UTF-8. It can be opened normally with IE.
When he discovered that the XML file he created did not have a BOM, an interesting thing was that when using UE to open such files containing UNICODE characters, UE will automatically add FF FE in front of the file so that the file can be displayed normally. , so if you browse a file that originally does not have a BOM in hexadecimal under UE, you will see an additional BOM. This function can be removed in the OPTIONS of UE. If you want to know, you can find it yourself.
Then I got a little confused, how could this happen, and then I thought and thought, and suddenly he sent a message with the following content:
W3C defines three pieces of XML Rules for how the parser correctly reads the encoding of XML files:
1. If the document has a BOM (Byte Order Mark, generally speaking, if it is saved in unicode format, it contains the BOM, but ANSI does not) , the file encoding is defined
2. If there is no BOM, check the encoding attribute of the XML declaration
3. If there are neither of the above, it is assumed that the XML document is encoded in UTF-8
With these three rules, this rule will be much clearer.
First, the XML parser parses the file according to the BOM of the file; if the BOM is not found, the encoding specified by the encoding attribute in XML is used; if the encoding is not specified in XML, utf-8 is used by default. Parse the document. Then it can be launched. If there are both BOM and ENCODING, the one specified by BOM shall prevail.
ah! Suddenly I felt how great it would be to have standard documents! Although it is so natural.
At this point, I finally understand the relationship between encoding and file format in xml. Although this record only contains a few hundred words, when we were discussing it, the total time spent was almost 2 hours.
The above is the detailed content of Detailed explanation of encoding in xml. For more information, please follow other related articles on the PHP Chinese website!