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. PostgreSQL11.2 configure卡住 checking for DocBook XML V4.2

    在PG11.2的数据库编译过程中,卡在了“checking for DocBook XML V4.2”,不动,需要安装docbook才可以. 需要安装: yum install docbook-dtd ...

  2. grafana查询中的变量templating

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

  3. UI基础:UIControl及其子类

    UISegmentedControl  UISegmentedControl 是iOS中的分段控件 每个segment 都能被点击,相当于集成了若干个button. 通常我们会点击不同的segment ...

  4. caffe编译问题-src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory compilation terminated.

    错误描述 src/caffe/net.:: fatal error: hdf5.h: No such : recipe 操作过程 step1: 在Makefile.config文件更改INCLUDE_ ...

  5. PDB调试模块

    这里主要是一些对于调试常用的命令:1.直接通过命令端输入进行调试 以pdb调试模式运行(主要用这个) python3 -m pdb file.py 2.在代码中导入pdb模块 import pdb 功 ...

  6. Spring Data JPA Hibernate @QueryHints

    另一个实例: http://leobluewing.iteye.com/blog/2032396 : 本文内容来源:https://blog.csdn.net/gavinchen1985/articl ...

  7. yocto和bitbake

    一.yocto 1.yocto简介 Yocto 是一个开源社区通过它提供模版.工具和方法帮助开发者创建基于linux内核的定制系统,支持ARM, PPC, MIPS, x86 (32 & 64 ...

  8. silverlight 中javascript 代码与托管代码的互调用 以及一些思考

    silverlight 客户端javascript 代码与托管代码的互调用时比较用意义的同时,因为silverlight本身就是一个插件,如果两者之间不能进行相互的调用,对于web 上的一些特殊的功能 ...

  9. linux和mac使用virtualenv使用和安装

    virtualenv是python的三大神器之一,用于创建独立的python虚拟环境,多个python版本相互独立,互不影响,可以在一台电 脑上同时安装多个版本的python,而且不影响本机pytho ...

  10. RabbitMQ之监控

    RabbitMQ作为一个工业级的消息中间件,肯定是缺少不了监控的,RabbitMQ提供了WEB版的页面监控(访问地址:http://xxx.xxx.xxx.xxx:15672/,默认端口号是15672 ...