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. Exchange Server 2007 多名称证书配置(转载)

    原文:http://www.exchangecn.com/exchange2007/20141130_529.html Exchange Server 2007 多名称证书配置   Exchange ...

  2. golang多核的使用

    实际上协程只是发生在单个进程内部的,要是想充分的发掘多核CPU的潜力,还是需要多进程的支持. 对于多核编程,go是天生支持,那么我们在什么情况下应该用多核心来加速程序,而在什么情况下用单核即可呢? 现 ...

  3. JAVA取得IP

    通常这样取一个IP:request.getRemoteAddr(),但有时候取出来的是不对的,特别是在应用前加了Nginx之类的软件,下面的方法可以取出访问的IP地址 public static St ...

  4. 【opencv基础】OpenCV从Mat中提取某些行或列

    这两个函数返回的是指向原矩阵内部位置的指针,类似于浅拷贝: code cv::Mat align_mean(cv::Mat mean, cv::Rect facebox, float scaling_ ...

  5. rm: 无法删除"xxxx.dir": 是一个目录

    rm命令 -f:在删除过程中不给任何指示,直接删除. -r:将参数中列出的全部目录和子目录都递归地删除. -i:与-f选项相反,交互式删除,在删除每个文件时都给出提示. 删除文件可以直接使用rm命令, ...

  6. SQL SERVER 2008R2 执行大脚本文件时,管理工具提示“内存不足”的解决方法

    项目需求:当我把服务器上的数据库导出为SQL脚本时,在本地新建数据库,再导入执行SQL文件时报错,因为SQL文件过大,导致出现如下报错 如下图所示: ========================= ...

  7. hdu1208 dp

    题意:给了一个 n * n 的方格图,要从图的左上角走到右下角 ,每次只能向右或者向下走,走的格数为当前格子上的数字,问共有多少中走法. 一开始我看到之后觉得这题完全可以用记忆化搜索来做,dfs 一遍 ...

  8. JSON与JAVA数据的转换-----从3,23到现在5.25才过去2个月,感觉时间过得那么漫长

    从3月23号去报到,期间经历了清明节,毕业论文答辩,从万达搬到东兴,五一节,毕业照,从东兴的一边搬到另外一个房间中去 2个月的时间过得如此的快啊!白驹过隙! 不要着急,不要和别人比,小龙哥写过3年代码 ...

  9. nuclio kubernetes 部署

    一张参考架构图: 从图中可以看到nuclio可以运行到docker 以及kubernetes中 提供了kubernetes 部署的脚本 安装 创建命名空间 kubectl create namespa ...

  10. Oracle C#处理时间类型的Insert

    首先如果直接   parm.Value=DateTime.Now;   insert into table (TheTime)Value(@parm);   执行sql就会报错 ----------- ...