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传输的文件

  1. Content-type: application/xml; charset="utf-16"
  2. {BOM}<?xml version="1.0"?>
  1. Content-type: application/xml; charset="utf-16"
  2. {BOM}<?xml version="1.0"?>

XML and MIME processors会按照utf-16编码处理该文件

例子2:

webservice传输的文件

  1. Content-type: application/xml
  2. <?xml version='1.0'?>
  1. Content-type: application/xml
  2. <?xml version='1.0'?>

XML processors会按照utf-8编码处理该文件

例子3:

webservice传输的文件

  1. Content-type: application/xml
  2. <?xml version='1.0' encoding="ISO-10646-UCS-4"?>
  1. Content-type: application/xml
  2. <?xml version='1.0' encoding="ISO-10646-UCS-4"?>

XML processors会按照UCS-4编码处理该文件

例子4:

webservice传输的文件

  1. Content-type: text/xml
  2. {BOM}<?xml version="1.0" encoding="utf-16"?>
  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

application/xml 和 text/xml的区别的更多相关文章

  1. application/xml和text/xml的区别

    XML有两个MIME类型,application/xml和text/xml,它们之间的区别是: text/xml忽略xml文件头中的关于编码的设定(<?xml version="1.0 ...

  2. 四种常见的 POST 提交数据方式(application/x-www-form-urlencoded,multipart/form-data,application/json,text/xml)

    四种常见的 POST 提交数据方式(application/x-www-form-urlencoded,multipart/form-data,application/json,text/xml) 转 ...

  3. 解决"415 Cannot process the message because the content type 'application/x-www-form-urlencoded' was not the expected type 'text/xml; charset=utf-8'"

    wcf basicHttpBinding content-type    text/xml;charset=utf-8 wsHttpBinding  'application/soap+xml; ch ...

  4. solr File Upload "Unsupported ContentType: application/vnd.ms-excel Not in: [application/xml, application/csv, application/json, text/json, text/csv, text/xml, application/javabin]",

    今天在用solr管理界面导入文件时报错:"Unsupported ContentType: application/vnd.ms-excel  Not in: [application/xm ...

  5. Webservice报错客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”。

    控制台对接Webservice正常,同样的方法在Web项目上报错: 客户端发现响应内容类型为“application/json;charset=UTF-8”,但应为“text/xml”.请求失败,错误 ...

  6. android 中theme.xml与style.xml的区别

    from://http://liangoogle.iteye.com/blog/1848448 android 中theme.xml与style.xml的区别: 相同点: 两者的定义相同. <r ...

  7. php接收json格式数据(text/xml)

    在API服务中,目前流行采用json形式来交互. 给前端调用的接口输出Json数据,这个比较简单,只需要组织好数据,用json_encode($array) 转化一下,前端就得到json格式的数据. ...

  8. "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."

    "SQL Server does not handle comparison of NText, Text, Xml, or Image data types." sql2000 ...

  9. MyBatis Mapper.xml文件中 $和#的区别

    MyBatis Mapper.xml文件中 $和#的区别   网上有很多,总之,简略的写一下,作为备忘.例子中假设参数名为 paramName,类型为 VARCHAR . 1.优先使用#{paramN ...

随机推荐

  1. ZEDGRAPH画图心得,SQL语句构造!!!

    /// <summary> /// 画折线 /// </summary> public void Drawline() { OleDbConnection odcConnect ...

  2. grafana查询中的变量templating

    有时我们在管理成百上千台机器的时候,配置grafana无疑是明智的,因为你不需要一个一个的把每个机器的图形都配置一遍,利用templating就可以瞬间实现n台机器的状态显示了. templating ...

  3. CSS 网格布局学习

    转自:https://blog.jirengu.com/?p=990 CSS网格布局(又名“网格”)是一个二维的基于网格的布局系统,其目的只在于完全改变我们设计基于网格的用户界面的方式. CSS一直用 ...

  4. Python装饰器的通俗理解

    转载:http://blog.csdn.net/u013471155 在学习Python的过程中,我相信有很多人和我一样,对Python的装饰器一直觉得很困惑,我也是困惑了好久,并通过思考和查阅才能略 ...

  5. parseConf(配置文件解析器)

    /****************************************************************************** * * parseConf(配置文件解析 ...

  6. # 20155327 2016-2017-4 《Java程序设计》第七周学习总结

    20155327 2016-2017-4 <Java程序设计>第七周学习总结 教材学习内容总结 了解Lambda语法 包含三个部分 一个括号内用逗号分隔的形式参数,参数是函数式接口里面方法 ...

  7. 蓝桥杯 BASIC-9:特殊回文数

      基础练习 特殊回文数   时间限制:1.0s   内存限制:512.0MB        问题描述 123321是一个非常特殊的数,它从左边读和从右边读是一样的. 输入一个正整数n, 编程求所有这 ...

  8. java1.8操作日期

    java1.8获取年份: int year = Calendar.getInstance().get(Calendar.YEAR); StringBuilder code = new StringBu ...

  9. Maven Dependency错误——下载失败问题解决方案

    问题描述: The container 'Maven Dependencies' references non existing library '${groupid}/${artifactid}-$ ...

  10. Java中的内存泄露