XML字符编码

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.
将文件保存为"UTF-16"编码格式

If you add an encoding attribute to XML files saved as Unicode, windows encoding values will generate an error.
如果你给XML文件加上编码属性,wendows的编码值会产生错误。

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.
如果编码属性指定为像Windows-1252, ISO-8859-1 或 UTF-8的单字节编码,而你的文件却是以Unicode/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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值