对于Web服务响应,text / xml与application / xml之间有什么区别

时间:2011-01-28 19:40:53

标签: xml rest jersey

这是关于text/xmlapplication/xml之间差异的一般性问题。 我很擅长编写webservices(REST - Jersey)。我一直在制作application/xml因为它出现在我一直用来学习的大多数教程/代码示例中,但我最近发现了text/xml,并且想知道它有什么不同之处你会在application/xml上使用它吗?

5 个答案:

答案 0 :(得分:394)

来自RFC(3023),在第3节“XML媒体类型:

”下
  

如果是XML文档 - 即未处理的源XML文档       - 临时用户可读 text / xml 更可取      应用程序/ XML。没有的MIME用户代理(和Web用户代理)      明确支持text / xml会将其视为text / plain,for      例如,通过将XML MIME实体显示为纯文本。      当XML MIME实体不可读时, Application / xml 更可取      由临时用户提供。

(强调我的)

答案 1 :(得分:72)

这是一个老问题,但现在可以从RFC 7303获取经常访问的一个明确的建议,它废弃了RFC3023。简而言之(第9.2节):

The registration information for text/xml is in all respects the same
as that given for application/xml above (Section 9.1), except that
the "Type name" is "text".

答案 2 :(得分:30)

根据this article申请/ xml是首选。


修改

我对这篇文章做了一些跟进。

作者声称在XML处理指令中声明了编码,如:

<?xml version="1.0" encoding="UTF-8"?>

在使用text/xml媒体类型时可以忽略。

他们在RFC 2046中使用text/* MIME类型系列规范的定义来支持论文,特别是以下片段:

4.1.2.  Charset Parameter

   A critical parameter that may be specified in the Content-Type field
   for "text/plain" data is the character set.  This is specified with a
   "charset" parameter, as in:

     Content-type: text/plain; charset=iso-8859-1

   Unlike some other parameter values, the values of the charset
   parameter are NOT case sensitive.  The default character set, which
   must be assumed in the absence of a charset parameter, is US-ASCII.

   The specification for any future subtypes of "text" must specify
   whether or not they will also utilize a "charset" parameter, and may
   possibly restrict its values as well.  For other subtypes of "text"
   than "text/plain", the semantics of the "charset" parameter should be
   defined to be identical to those specified here for "text/plain",
   i.e., the body consists entirely of characters in the given charset.
   In particular, definers of future "text" subtypes should pay close
   attention to the implications of multioctet character sets for their
   subtype definitions.

根据他们的说法,使用application/xml MIME类型时可以避免这些困难。无论是否真实,我都不会避免text/xml。恕我直言,最好只是遵循人类可读性(不可读性)的语义,并始终记住指定字符集。

答案 3 :(得分:5)

application/xmlsvn视为二进制类型,而text/xml被视为文本文件,可以显示差异。

答案 4 :(得分:0)

不是回答您的问题,而是提供一种简单的生活:

当您生活在.NET Framework生态系统中时->请查看https://referencesource.microsoft.com/#system.web/MimeMapping.cs第〜430行:

AddMapping(".xml", "text/xml");

所以你总是可以做

string mimeType = System.Web.MimeMapping.GetMimeMapping(string yourFileName)

正确获取您的模仿类型