当前位置: 首页 > 网络学院 > XML相关教程 > XML > XML字符编码

XML
XML DHTML行为
XML 相关技术
XML 编辑器
XML 摘要
XML 实例
XML字符编码
xml 文档树
IE和火狐读取XML方法比较

XML 中的 XML字符编码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 737 ::
收藏到网摘: n/a

XML documents may contain foreign characters, like Norwegian ???, or French ???
XML文档中可以使用外来字符,比如挪威语或法语。

To let your XML parser understand these characters, you should save your XML documents as Unicode.
如要你希望XML解析器能够读懂这些字符,你就应该将XML文档保存为“统一字符编码(Unicode)”。


Windows 2000 记事本

Windows 2000 Notepad can save files as Unicode.
WIN2000记事本可以将文件以“统一字符编码(Unicode)”的形式保存。

Save the XML file below as Unicode (note that the document does not contain any encoding attribute):
将下述的XML文件以“统一字符编码(Unicode)”的形式保存(注意:文档中不能包含任何编码属性):

<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Norwegian: 骧? French: 觇?lt;/message>
</note>

The file above, note_encode_none_u.xml will NOT generate an error in IE 5+, Firefox, or Opera, but it WILL generate an error in Netscape 6.2.
上述文件在IE5+、Firefox 或Opera中不会产生任何错误,但它在Netscape 6.2 中会产生一个错误。


Windows 2000 记事本附带编码

Windows 2000 Notepad files saved as Unicode use "UTF-16" encoding.
WIN2000记事本可以使用"UTF-16"编码格式将文件以“统一字符编码(Unicode)”的形式保存。

If you add an encoding attribute to XML files saved as Unicode, windows encoding values will generate an error.
如果你给以“统一字符编码(Unicode)”的形式保存的XML文件加上一个编码属性,那么,windows的编码值会产生错误。

The following encoding (open it), will NOT give an error message:
使用下面的编码,不会产生任何错误:

<?xml version="1.0" encoding="windows-1252"?>

The following encoding (open it), will NOT give an error message:
使用下面的编码,不会产生任何错误:

<?xml version="1.0" encoding="ISO-8859-1"?>

The following encoding (open it), will NOT give an error message:
使用下面的编码,不会产生任何错误:

<?xml version="1.0" encoding="UTF-8"?>

The following encoding (open it), will NOT generate an error in IE 5+, Firefox, or Opera, but it WILL generate an error in Netscape 6.2.
使用下面的编码在IE 5+、Firefox 或 Opera 中不会产生任何错误,但在Netscape 6.2中会产生错误:

<?xml version="1.0" encoding="UTF-16"?>

 


错误信息

If you try to load an XML document into Internet Explorer, you can get two different errors indicating encoding problems:
如果你尝试把XML文档载入Internet Explorer,你会收到关于编码问题的两个不同的错误信息:

An invalid character was found in text content.
在文本内容中发现一个无效字符。

You will get this error message if a character in the XML document does not match the encoding attribute. Normally you will get this error message if your XML document contains "foreign" characters, and the file was saved with a single-byte encoding editor like Notepad, and no encoding attribute was specified.
如果XML文档中有一个字符与编码属性不匹配,你就会收到这个错误信息。通常情况下,如果你的XML的文档包含外文字符,或者,文件是使用类似于记事本这样的单字节编辑器来保存的,再或者,编码属性没有被指定,那么,你就会收到这个错误信息。

Switch from current encoding to specified encoding not supported.
从当前的编码切换到未被支持的指定编码

You will get this error message if your file was saved as Unicode/UTF-16 but the encoding attribute specified a single-byte encoding like Windows-1252, ISO-8859-1 or  UTF-8. You can also get this error message if your document was saved with single-byte encoding, but the encoding attribute specified a double-byte encoding like UTF-16.
如果你的文件是以“ Unicode/UTF-16 ” 来保存的,但是,它们的编码属性指定为诸如:“Windows-1252”、“ISO-8859-1”或“UTF-8”这样的单字节编码,那么,你会收到错误信息;如果你的文档使用单字节编码形式保存,但是,编码属性指定为诸如:“UTF-16”这样的双字节编码,你仍然会收到这个错误信息。


结论

The conclusion is that the encoding attribute has to specify the encoding used when the document was saved. My best advice to avoid errors is:
结论:当文件被保存时,编码属性应正确指定被使用的编码。为了避免错误的发生,我提出的建议是:

  • Use an editor that supports encoding
    使用支持编码的编辑器
  • Make sure you know what encoding it uses
    确保你对所使用的编码了如指掌
  • Use the same encoding attribute in your XML documents
    在你的XML文档中使用相同的编码属性

评论 (0) All

登陆 | 还没注册?