application/xml and text/xml的區別
經常看到有關xml時提到"application/xml" 和 "text/xml"兩種類型, 二者功能一模一樣,唯一的區別就是編碼格式,text/xml忽略xml頭所指定編碼格式而默認采用us-ascii編碼,而application/xml會根據xml頭指定的編碼格式來編碼:
XML has two MIME types,application/xml and text/xml . These are often used interchangeably, but there is a subtle difference which is why application/xml is generally recommended over the latter.
Let me explain why: according to the standard, text/* -MIME types have a us-ascii character set unless otherwise specified in the HTTP headers. This effectively means that any encoding defined in the XML prolog (e.g. <?xml version=”1.0” encoding=”UTF-8”?>) is ignored. This is of course not the expected and desired behaviour.
To further complicate matters, most/all browser implementations actually implement nonstandard behaviour for text/xml because they process the encoding as if it were application/xml .
So, text/* has encoding issues, and is not implemented by browsers in a standards-compliant manner, which is why using application/* is recommended.
text/xml 和 application/xml的字符集編碼問題
關鍵字: text/xml application/xml
對於Webservice的應用來說,我們通常都是用UTF-8進行網絡傳輸,但也有通過GBK和GB2312傳輸的情況,但是在我們Webservice的代碼實現中,其實是不用關心具體的傳輸編碼的,因為根據RFC2376的定義,Webservice的引擎(axis,cxf,jaxws..)會根據文件傳輸的ContentType及XML 聲明部分定義的編碼自動將網絡傳輸過來的內容(字符串)轉換成unicode(jvm運行時的字符串都是以unicode形式存在的)。以下是RFC2376的描述:
例子1:
webservice傳輸的文件
- Content-type: application/xml; charset="utf-16"
- {BOM}<?xml version="1.0"?>
- Content-type: application/xml; charset="utf-16"
- {BOM}<?xml version="1.0"?>
XML and MIME processors會按照utf-16編碼處理該文件
例子2:
webservice傳輸的文件
- Content-type: application/xml
- <?xml version='1.0'?>
- Content-type: application/xml
- <?xml version='1.0'?>
XML processors會按照utf-8編碼處理該文件
例子3:
webservice傳輸的文件
- Content-type: application/xml
- <?xml version='1.0' encoding="ISO-10646-UCS-4"?>
- Content-type: application/xml
- <?xml version='1.0' encoding="ISO-10646-UCS-4"?>
XML processors會按照UCS-4編碼處理該文件
例子4:
webservice傳輸的文件
- Content-type: text/xml
- {BOM}<?xml version="1.0" encoding="utf-16"?>
- Content-type: text/xml
- {BOM}<?xml version="1.0" encoding="utf-16"?>
XML processors會按照us-ascii,而不是utf-16編碼處理該文件
參考文檔: