您的位置:首页 > Web前端 > JavaScript

Comparing JSON and XML? Stop it!

2016-04-28 20:28 489 查看
中国人似乎特别喜欢比较。总会有百无聊赖的提问:

哪种编程语言更好?

是写服务端好还是客户端好?

是Android好还是ios好?

是桌面端好还是移动端好还是网页端好?

等等……

我猜这样的人最后什么都学不成,更多的情况是C语言都不了解,一行代码不会写,就提出这样的问题。

============================================================

现在开始正文:

做c/s开发的人,即使你一定不懂JavaScript,那你也一定听说过JSON,也一定听说过XML。

这是两种人们常常采用的存储数据的格式。

那么国人的本性发作了:who is better?

JSON 和 XML谁更好呢? 谁更快呢?我到底该使用哪一个呢?就像比较自行车和汽车哪个更好一样?

先什么都不说,看个片段:

{
"id": 110,
"title": "Living or Die",
"author": "Wang Shubo",
"published": {
"by": "gcd",
"year": 2016
}
}


<?xml version="1.0"?>
<book id="110">
<title>Living or Die</title>
<author>Wang Shubo</author>
<published>
<by>gcd</by>
<year>2016</year>
</published>
</book>


上面两个片段就是最基本的xml和json结构,谁能根据这个就判断who is better!!!

第一个片段共计140个字母,第二个片段167个字母,根据这个就能判断who is better?

再看看他们的优缺点:

JSON

优点:

Simple syntax, which results in less “markup” overhead compared to XML.

Easy to use with JavaScript as the markup is a subset of JS object literal notation and has the same basic data types as JavaScript.

JSON Schema for description and datatype and structure validation

缺点:

Simple syntax, only a handful of different data types are supported.

支持的数据类型少。

XML

优点:

Generalized markup; it is possible to create “dialects” for any kind of purpose

XML Schema for datatype, structure validation. Makes it also possible to create new datatypes

XSLT for transformation into different output formats

XPath/XQuery for extracting information (which makes getting information in deeply nested structures much easier then with JSON)

built in support for namespaces

缺点:

Relatively wordy compared to JSON (results in more data for the same amount of information).

支持更多的类型,可以添加更多的信息。

还有~

需要明确的是:

JSON is a good data format, and it is just a data format

所以,就是说很多情况下json是不能替代xml的:

XML is not a data format; it is a language. A very powerful one

从foreigner的一篇文章趴下来的理由:链接 http://www.yegor256.com/2015/11/16/json-vs-xml.html

XPath.

To get data like the year of publication from the document above, I just send an XPath query: /book/published/year/text(). However, there has to be an XPath processor that understands my request and returns 2004. The beauty of this is that XPath 2.0 is a very powerful query engine with its own functions, predicates, axes, etc. You can literally put any logic into your XPath request without writing any traversing logic in Java, for example. You may ask “How many books were published by David West in 2004?” and get an answer, just via XPath. JSON is not even close to this.

Attributes and Namespaces.

You can attach metadata to your data, just like it’s done above with the id attribute. The data stays inside elements, just like the name of the book author, for example, while metadata (data about data) can and should be placed into attributes. This significantly helps in organizing and structuring information. On top of that, both elements and attributes can be marked as belonging to certain namespaces. This is a very useful technique during times when a few applications are working with the same XML document.

XML Schema.

When you create an XML document in one place, modify it a few times somewhere else, and then transfer it to yet another place, you want to make sure its structure is not broken by any of these actions. One of them may use to store the publication date while another uses with ISO-8601. To avoid that mess in structure, create a supplementary document, which is called XML Schema, and ship it together with the main document. Everyone who wants to work with the main document will first validate its correctness using the schema supplied. This is a sort of integration testing in production. RelaxNG is a similar but simpler mechanism; give it a try if you find XML Schema too complex.

XSL.

You can make modifications to your XML document without any Java/Ruby/etc. code at all. Just create an XSL transformation document and “apply” it to your original XML. As an output, you will get a new XML. The XSL language (it is purely functional, by the way) is designed for hierarchical data manipulations. It is much more suitable for this task than Java or any other OOP/procedural approach. You can transform an XML document into anything, including plain text and HTML. Some complain about XSL’s complexity, but please give it a try. You won’t need all of it, while its core functionality is pretty straight-forward.

最后的最后,本想自己总结一下,但是在stackoverflow上看到了更好的,很到位:

如果你只是想简单的传递atomic values,json比xml好用了太多:json可以直接用在Internet上,被很多应用程序广泛的支持,而且可以写很简单的程序去处理json,json容易被创建,而且使用的是Unicode编码。

对于非结构化的数据,我们就要使用xml

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

彩蛋:

I’m not worried about the future of XML at all even if its death is gleefully celebrated by a cadre of web API designers.

And I can’t resist tucking an “I told you so!” token away in my desk. I look forward to seeing what the JSON folks do when they are asked to develop richer APIs. When they want to exchange less well strucured data, will they shoehorn it into JSON? I see occasional mentions of a schema language for JSON, will other languages follow? …

最后,看到了一个人说的比较有意思:

抵制xml的大多是Web Developers,而不是integration developers。

笑而不语~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: