java XML 与 JSON。哪个更适合存储小块数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15154049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:39:32  来源:igfitidea点击:

XML vs JSON. Which one is better for storing small chunk of data?

javaandroidxmldatabasejson

提问by KunalK

I want to store some small chunk of data and don't want to go for any database, we have two choices XMLand JSON, now anyone can please suggest which one should I select from performance and architecture point of view.

我想存储数据的一些小块的,不想去任何数据库中,我们有两个选择XMLJSON,现在任何人都可以请建议,人们应该从我的观点的性能和架构点选择。

1. which is better to use? XML or JSON for storing data?

1. 哪个更好用?XML 或 JSON 用于存储数据?

2. What are the pros and cons for both JSON and XML?

2. JSON 和 XML 的优缺点是什么?

any help would be greatly appreciated.

任何帮助将不胜感激。

EDIT

编辑

We are not using any web service, our application is a stand alone app. We want to use XML or JSON for storing some local data which will be used in the application. The data would be like details of questions and answers, static userdetails etc.

我们没有使用任何网络服务,我们的应用程序是一个独立的应用程序。我们想使用 XML 或 JSON 来存储一些将在应用程序中使用的本地数据。数据就像问题和答案的详细信息、静态用户详细信息等。

回答by Danny

Please keep in mind that JSON is only smaller if the tags are longer than the data. Probably the fact that the XML is a lot easier to read, and that JSON has a smaller footprint.

请记住,只有当标签比数据长时,JSON 才会更小。可能是因为 XML 更容易阅读,而且 JSON 占用空间更小。

XML Pros

XML 优点

  • Easier to read
  • Used a lot more than JSON
  • One of the main industry standards
  • Versioning possible
  • Namespace support
  • Multiple elements with the same name
  • Validation
  • 更容易阅读
  • 使用的比 JSON 多得多
  • 主要行业标准之一
  • 可以进行版本控制
  • 命名空间支持
  • 多个同名元素
  • 验证

XML Cons

XML 缺点

  • Takes up more space
  • Increased bandwidth because of the size
  • 占用更多空间
  • 由于尺寸增加了带宽

JSON Pros

JSON 优点

  • Doesn't take up a lot of space
  • Uses less bandwidth because of it's size (footprint)
  • Rising in the ranks as one of the main industry standards
  • 不占用太多空间
  • 由于它的大小(占用空间)而使用更少的带宽
  • 作为主要行业标准之一在行列中不断上升

JSON Cons

JSON 的缺点

  • Harder to read
  • Versioning breaks client/data
  • 更难读
  • 版本控制破坏客户端/数据

If you are sending more data than you send tags then they are about the same and you would have been better off using XML for the fast parsing speeds. I would also argue that people expect slow mobile load times and fast app running times so try and not slow down the app time by using a slower format to parse.

如果您发送的数据多于发送标签的数据,那么它们大致相同,您最好使用 XML 来实现快速的解析速度。我还要争辩说,人们期望缓慢的移动加载时间和快速的应用程序运行时间,因此尽量不要通过使用较慢的格式进行解析来减慢应用程序的时间。

Finally I say JSON, The small footprint will speed up transactions between your app and the web services you're trying to send/receive data to/from.

最后我说JSON,占用空间小将加快您的应用程序和您尝试向其发送/接收数据的 Web 服务之间的事务。

回答by Dinesh Prajapati

JSON is the best way to design any mobile application development, because parsing JSON is very light weight operation compare to XML. while XML parsing is always leads to complex memory problem. and JSON can be easily build/parse with GSON library which is again very light weight.

JSON 是设计任何移动应用程序开发的最佳方式,因为与 XML 相比,解析 JSON 是非常轻量级的操作。而 XML 解析总是会导致复杂的内存问题。并且可以使用 GSON 库轻松构建/解析 JSON,这也是非常轻量级的。

XML Parsing will be head ache if you have different versions of parsers to use. so go for JSON.

如果您要使用不同版本的解析器,XML 解析将令人头疼。所以去JSON。

回答by Erol

Extensible Markup Language (XML) is a text format derived from Standard Generalized Markup Language (SGML).

可扩展标记语言 (XML) 是一种源自标准通用标记语言 (SGML) 的文本格式。

Most of the excitement around XML is around a new role as an interchangeable data serialization format. XML provides two enormous advantages as a data representation language:

围绕 XML 的大部分兴奋都围绕着作为可互换数据序列化格式的新角色。XML 作为一种数据表示语言提供了两个巨大的优势:

It is text-based.
It is position-independent.

These together encouraged a higher level of application-independence than other data-interchange formats. The fact that XML was already a W3C standard meant that there wasn't much left to fight about (or so it seemed).

与其他数据交换格式相比,这些共同鼓励了更高水平的应用程序独立性。XML 已经是 W3C 标准这一事实意味着没有太多可争论的(或者看起来如此)。

Unfortunately, XML is not well suited to data-interchange, much as a wrench is not well-suited to driving nails. It carries a lot of baggage, and it doesn't match the data model of most programming languages. When most programmers saw XML for the first time, they were shocked at how ugly and inefficient it was. It turns out that that first reaction was the correct one. There is another text notation that has all of the advantages of XML, but is much better suited to data-interchange. That notation is JavaScript Object Notation (JSON).

不幸的是,XML 不太适合数据交换,就像扳手不太适合钉钉一样。它背负着很多包袱,与大多数编程语言的数据模型不符。大多数程序员第一次看到 XML 时,都对它的丑陋和低效感到震惊。事实证明,第一反应是正确的。还有另一种文本表示法具有 XML 的所有优点,但更适合于数据交换。该表示法是 JavaScript 对象表示法 (JSON)。

The most informed opinions on XML (see for example xmlsuck.org) suggest that XML has big problems as a data-interchange format, but the disadvantages are compensated for by the benefits of interoperability and openness.

关于 XML 的最明智的意见(例如参见 xmlsuck.org)表明 XML 作为一种数据交换格式存在很大的问题,但其缺点被互操作性和开放性的好处所弥补。

JSON promises the same benefits of interoperability and openness, but without the disadvantages.

JSON 承诺互操作性和开放性的相同好处,但没有缺点。

Rest of the comparison is here.

其余的比较在这里