有效的 XML 文件是否需要 XML 声明? [英] Does a valid XML file require an XML declaration?

查看:31
本文介绍了有效的 XML 文件是否需要 XML 声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xerces 的 Sax Parser 解析 XML 文件.
是否需要 XML 声明 <?xml version="1.0" encoding="UTF-8"?>?

I am parsing an XML file using Sax Parser of Xerces.
Is the XML declaration <?xml version="1.0" encoding="UTF-8"?> required?

推荐答案

在 XML 1.0 中,XML 声明可选的.请参阅XML 1.0 建议的第 2.8 节,其中说应该"被使用——这意味着它是推荐的,但不是强制性的.但是,在 XML 1.1 中,声明是强制.请参阅XML 1.1 建议的第 2.8 节,其中说必须"使用.它甚至继续声明如果声明不存在,则自动暗示该文档是 XML 1.0 文档.

In XML 1.0, the XML Declaration is 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 if the declaration is absent, that automatically implies the document is an XML 1.0 document.

请注意,在 XML 声明中,encodingstandalone 都是可选的.只有 version 是强制性的.此外,这些不是属性,因此如果它们存在,它们必须按以下顺序排列:version,然后是任何 encoding,然后是任何 standalone.

Note that in an XML Declaration the encoding and standalone are both optional. Only the version is 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 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"?>

如果不以这种方式指定编码,XML 解析器会尝试猜测正在使用的编码.XML 1.0 Recommendation 描述了一种可以自动检测字符编码的可能方法.实际上,如果输入被编码为 UTF-8、UTF-16 或 US-ASCII,这不是什么大问题.当遇到使用 US-ASCII 范围之外的字符(例如 ISO 8859-1)的 8 位编码时,自动检测不起作用 - 如果可以,请避免创建这些.

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.

standalone 表示 XML 文档是否可以在没有 DTD 的情况下正确处理.人们很少使用它.如今,设计一种缺少信息而没有 DTD 的 XML 格式是很糟糕的.

The standalone indicates 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.

更新:

prolog 错误/无效的 utf-8 编码"错误表示解析器在文件中找到的实际数据与 XML 声明所说的编码不匹配.或者在某些情况下,文件中的数据与自动检测的编码不匹配.

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.

由于您的文件包含字节顺序标记 (BOM),因此它应该采用 UTF-16 编码.我怀疑您的声明说 <?xml version="1.0" encoding="UTF-8"?> 当记事本将文件更改为 UTF-16 时,这显然是不正确的.简单的解决方案是删除 encoding 并简单地说 <?xml version="1.0"?>.您也可以将其编辑为 encoding="UTF-16" 但这对于原始文件(不在 UTF-16 中)或者文件以某种方式改回 UTF 是错误的-8 或其他一些编码.

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 encoding and 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——这不是问题的原因.使用记事本或写字板编辑 XML 才是真正的问题!

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!

这篇关于有效的 XML 文件是否需要 XML 声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆