首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

xml怎么探测字符编码

2012-09-18 
xml如何探测字符编码xml如何探测字符编码之前一度以为,xml头部的字符声明部分,即:NameUTF-8UTF-16UTF-16BE

xml如何探测字符编码
xml如何探测字符编码

之前一度以为,xml头部的字符声明部分,即:

NameUTF-8UTF-16UTF-16BEUTF-16LEUTF-32UTF-32BEUTF-32LESmallest code point0000000000000000000000000000Largest code point10FFFF10FFFF10FFFF10FFFF10FFFF10FFFF10FFFFCode unit size8 bits16 bits16 bits16 bits32 bits32 bits32 bitsByte orderN/A<BOM>big-endianlittle-endian<BOM>big-endianlittle-endianFewest bytes per character1222444Most bytes per character4444444

可见,UTF-8和16是变长的,前者更通用,也相对节省空间。

概念上可以把UCS-4等价于UTF-32,而实际上后者是前者的子集。

关于endian的典故很有意思,大部分命名都是很形象的(如gc里面的eden)只是我们不理解人家的文化背景。

为什么在java,html,css,js里转义unicode的方式各不相同?

在上面的FAQ里,看到了这段也摘录于此:

Q: Is there a standard method to package a Unicode character so it fits an 8-Bit ASCII stream?

A: There are three or four options for making Unicode fit into an 8-bit format.

a) Use UTF-8. This preserves ASCII, but not Latin-1, because the characters >127 are different from Latin-1. UTF-8 uses the bytes in the ASCII only for ASCII characters. Therefore, it works well in any environment where ASCII characters have a significance as syntax characters, e.g. file name syntaxes, markup languages, etc., but where the all other characters may use arbitrary bytes. 
Example: “Latin Small Letter s with Acute” (015B) would be encoded as two bytes: C5 9B.

b) Use Java or C style escapes, of the form \uXXXXX or \xXXXXX. This format is not standard for text files, but well defined in the framework of the languages in question, primarily for source files.
Example: The Polish word “wyj?cie” with character “Latin Small Letter s with Acute” (015B) in the middle (? is one character) would look like: “wyj\u015Bcie".

c) Use the &#xXXXX; or &#DDDDD; numeric character escapes as in HTML or XML. Again, these are not standard for plain text files, but well defined within the framework of these markup languages.
Example: “wyj?cie” would look like “wyj&#x015B;cie"

d) Use SCSU. This format compresses Unicode into 8-bit format, preserving most of ASCII, but using some of the control codes as commands for the decoder. However, while ASCII text will look like ASCII text after being encoded in SCSU, other characters may occasionally be encoded with the same byte values, making SCSU unsuitable for 8-bit channels that blindly interpret any of the bytes as ASCII characters.
Example: “<SC2> wyj?cie” where <SC2> indicates the byte 0x12 and “?” corresponds to byte 0xDB. [AF]


结论:

java / json: \uXXXXcss:\XXXXhtml / xml:&#XXXX

xml的解析方式主要有两种方式:DOM和SAX,前者面向文档,在内存中映射出整个树状结构;后者面向事件,解析往往是一次性的,当然效率也更高。JAXP是java世界解析xml的的统一入口,意图是避免依赖特定厂商的接口。其提供了DOM和SAX两种解析方式,默认实现是apache的xerces。若要更换默认实现,可以参考这段:
    Use the javax.xml.parsers.SAXParserFactory system property.Use the properties file "lib/jaxp.properties" in the JRE directory. This configuration file is in standard java.util.Properties format and contains the fully qualified name of the implementation class with the key being the system property defined above. The jaxp.properties file is read only once by the JAXP implementation and it's values are then cached for future use. If the file does not exist when the first attempt is made to read from it, no further attempts are made to check for its existence. It is not possible to change the value of any property in jaxp.properties after it has been read for the first time.Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.parsers.SAXParserFactory in jars available to the runtime.Platform default SAXParserFactory instance.看起来只有service api比较通用,查到一篇文章以后有机会再细看。

热点排行