今天在代码里面看见一串非常奇怪的推断语句
if (c < 0x9 || c > 0x9 && c < 0xA || c > 0xA && c < 0xD || c > 0xD && c
< 0x20 || c > 0xD7FF && c < 0xE000 || c > 0xFFFD)
于是用0xE000和0xFFFD 搜索了一下,发现原来xml还有这些限制。
依据xml spec
http://www.w3.org/TR/xml/#charsets
xml合法的字符范围是
[2] | Char |
::= | #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF] |
然后有网上各种人遇到的问题
“由于字节数组中存在 Unicode: 0x0,而这个字节在Xml中被觉得是非法字符。对于一些经过编码或加、解密的字符串中,非常easy会出现这个 0x0,特别是在加、解密中。常常会涉及到字符填充。而填充物一般是 0x0。对于0x00-0x20 都会引起一定的问题,又由于这些字符不可见,因此用通常的编辑器进行编辑的时候找不到问题所在。
xml中须要过滤的字符分为两类:
一类是不同意出如今xml中的字符。这些字符不在xml的定义范围之内;
还有一类是xml自身要使用的字符。假设内容中有这些字符则需被替换成别的字符。
第一类字符:
对于第一类字符,我们能够通过W3C的XML文档来查看都有哪些字符不被同意出如今xml文档中。
XML同意的字符范围是“#x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]”。因此我们能够把这个范围之外的字符过滤掉。
第二类字符:
对于第二类字符一共同拥有5个。例如以下:
字符 HTML字符 字符编码
和(and) & & &
单引號 ’ ' '
双引號 ” " "
大于号 > > >
小于号 < < <
以及emoji问题 http://blog.csdn.net/u014542833/article/details/22954893
这仅仅能用一个非严格标准的xml解析器,或者用cdata包超出范围的字符了?