application/xml 和 text/xml的區別


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傳輸的文件

Xml代碼  復制代碼  收藏代碼
  1. Content-type: application/xml; charset="utf-16"  
  2.   {BOM}<?xml version="1.0"?>   
Xml代碼   收藏代碼
  1. Content-type: application/xml; charset="utf-16"  
  2.   {BOM}<?xml version="1.0"?>   

XML and MIME processors會按照utf-16編碼處理該文件

 

例子2:

 webservice傳輸的文件

Xml代碼  復制代碼  收藏代碼
  1. Content-type: application/xml   
  2.    <?xml version='1.0'?>  
Xml代碼   收藏代碼
  1. Content-type: application/xml  
  2.    <?xml version='1.0'?>  

XML processors會按照utf-8編碼處理該文件

 

例子3:

 webservice傳輸的文件

Xml代碼  復制代碼  收藏代碼
  1. Content-type: application/xml   
  2.    <?xml version='1.0' encoding="ISO-10646-UCS-4"?>  
Xml代碼   收藏代碼
  1. Content-type: application/xml  
  2.    <?xml version='1.0' encoding="ISO-10646-UCS-4"?>  

 XML processors會按照UCS-4編碼處理該文件
 

例子4:

 webservice傳輸的文件

Xml代碼  復制代碼  收藏代碼
  1. Content-type: text/xml   
  2.    {BOM}<?xml version="1.0" encoding="utf-16"?>  
Xml代碼   收藏代碼
  1. Content-type: text/xml  
  2.    {BOM}<?xml version="1.0" encoding="utf-16"?>  

 XML processors會按照us-ascii,而不是utf-16編碼處理該文件

 

參考文檔:

http://www.ietf.org/rfc/rfc2376.txt


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM