有效的 XML 文件是否需要 XML 声明?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7007427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does a valid XML file require an XML declaration?
提问by eros
I am parsing an XML file using Sax Parser of Xerces.
Is the XML declaration <?xml version="1.0" encoding="UTF-8"?>required?
我正在使用 Xerces 的 Sax Parser 解析 XML 文件。
是否<?xml version="1.0" encoding="UTF-8"?>需要XML 声明?
回答by Hoylen
In XML 1.0, the XML Declarationis optional. See section 2.8 of the XML 1.0 Recommendation, where it says it "should" be used -- which means it is recommended, but not mandatory. In XML 1.1, however, the declaration is mandatory. See section 2.8 of the XML 1.1 Recommendation, where it says "MUST" be used. It even goes on to state that ifthe declaration is absent, that automatically implies the document is an XML 1.0 document.
在 XML 1.0 中,XML 声明是可选的。请参阅XML 1.0 Recommendation 的第 2.8 节,其中说“应该”使用它——这意味着它是推荐的,但不是强制性的。然而,在 XML 1.1 中,声明是强制性的。请参阅XML 1.1 Recommendation 的第 2.8 节,其中说“必须”使用。它甚至继续声明,如果声明不存在,则自动暗示该文档是 XML 1.0 文档。
Note that in an XML Declarationthe encodingand standaloneare both optional. Only the versionis mandatory. Also, these are not attributes, so if they are present they must be in that order: version, followed by any encoding, followed by any standalone.
请注意,在XML 声明中,encoding和standalone都是可选的。只有version是强制性的。此外,这些不是属性,因此如果它们存在,它们必须按以下顺序排列:version,然后是 any encoding,然后是 any standalone。
<?xml version="1.0"?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" standalone="yes"?>
<?xml version="1.0" encoding="UTF-16" standalone="yes"?>
If you don't specify the encoding in this way, XML parsers try to guess what encoding is being used. The XML 1.0 Recommendation describes one possible way character encoding can be autodetected. In practice, this is not much of a problem if the input is encoded as UTF-8, UTF-16 or US-ASCII. Autodetection doesn't work when it encounters 8-bit encodings that use characters outside the US-ASCII range (e.g. ISO 8859-1) -- avoid creating these if you can.
如果不以这种方式指定编码,XML 解析器会尝试猜测正在使用的编码。XML 1.0 Recommendation 描述了一种可以自动检测字符编码的可能方式。实际上,如果输入被编码为 UTF-8、UTF-16 或 US-ASCII,这不是什么大问题。当遇到使用 US-ASCII 范围(例如 ISO 8859-1)之外的字符的 8 位编码时,自动检测不起作用 - 如果可以,请避免创建这些。
The standaloneindicates whether the XML document can be correctly processed without the DTD or not. People rarely use it. These days, it is a bad to design an XML format that is missing information without its DTD.
的standalone指示是否将XML文档可被正确地处理,而不DTD或没有。人们很少使用它。现在,设计一种缺少信息而没有 DTD 的 XML 格式是很糟糕的。
Update:
更新:
A "prolog error/invalid utf-8 encoding" error indicates that the actual data the parser found inside the file did not match the encoding that the XML declaration says it is. Or in some cases the data inside the file did not match the autodetected encoding.
“prolog 错误/无效的 utf-8 编码”错误表示解析器在文件中找到的实际数据与 XML 声明所说的编码不匹配。或者在某些情况下,文件内的数据与自动检测的编码不匹配。
Since your file contains a byte-order-mark (BOM) it should be in UTF-16 encoding. I suspect that your declaration says <?xml version="1.0" encoding="UTF-8"?>which is obviously incorrect when the file has been changed into UTF-16 by NotePad. The simple solution is to remove the encodingand simply say <?xml version="1.0"?>. You could also edit it to say encoding="UTF-16"but that would be wrong for the original file (which wasn't in UTF-16) or if the file somehow gets changed back to UTF-8 or some other encoding.
由于您的文件包含字节顺序标记 (BOM),因此它应该采用 UTF-16 编码。我怀疑您的声明说<?xml version="1.0" encoding="UTF-8"?>当记事本将文件更改为 UTF-16 时,这显然是不正确的。简单的解决方案是删除encoding并简单地说<?xml version="1.0"?>。您也可以对其进行编辑,encoding="UTF-16"但对于原始文件(不是 UTF-16)或者文件以某种方式改回 UTF-8 或其他一些编码,这将是错误的。
Don't bother trying to remove the BOM -- that's not the cause of the problem. Using NotePad or WordPad to edit XML is the real problem!
不要费心试图删除 BOM——这不是问题的原因。使用 NotePad 或 WordPad 编辑 XML 才是真正的问题!
回答by Aravind Yarram
Xml declaration is optional so your xml is well-formed without it. But it is recommended to use it so that wrong assumptions are not made by the parsers, specifically about the encoding used.
Xml 声明是可选的,因此没有它您的 xml 格式良好。但是建议使用它,这样解析器就不会做出错误的假设,特别是关于所使用的编码。
回答by Quentin
It is only required if you aren't using the default values for versionand encoding(which you are in that example).
仅当您不使用versionand的默认值encoding(您在该示例中使用)时才需要它。

