Home > Backend Development > XML/RSS Tutorial > JSON and XML-detailed explanation of how they are different

JSON and XML-detailed explanation of how they are different

黄舟
Release: 2017-03-17 17:04:50
Original
1489 people have browsed it

[Editor's Note] The author of this article, Yegor Bugayenko, is the co-founder of Teamed.io and has in-depth research in the fields of software quality and engineering management methods. In this article, the author explains the four major features of XML in more detail by comparing JSON to help you choose the appropriate data format type when building a project.

Many people are entangled in their hearts. If JSON and XML are compared, who is better and who is faster? Which one should you choose for your next new project? Do not be silly! There is absolutely no comparison. Like the bike and the AMG S65, can you say which one is better? Although both are means of transportation, in some cases, bicycles are more convenient. So the same goes for JSON and XML. They both have their own merits and there is no need to compare them at all.

The following is a simple JSON data (140 characters):

{
  "id": 123,
  "title": "Object Thinking",
  "author": "David West",
  "published": {
    "by": "Microsoft Press",
    "year": 2004
  }
}
Copy after login

The same data will be represented in XML as follows (167 characters):

<?xml version="1.0"?>
<book id="123">
  <title>Object Thinking</title>
  <author>David West</author>
  <published>
    <by>Microsoft Press</by>
    <year>2004</year>
  </published>
</book>
Copy after login

It’s easy See the difference. The former is simpler and easier to understand, and can be parsed perfectly in JavaScript. So, can we simply give up XML and choose JSON? Who needs a 15-year-old heavyweight language?

But on the contrary, I like XML very much, and let me explain it below.

But don’t get me wrong, this article is not against JSON. JSON is indeed a great data format, but it is just a data format that is often used ad hoc to get data from point A to point B. It's shorter and more readable than XML, but that's about it.

XML is a very powerful language, not just a simple data format. Compared with JSON and other simple data formats, such as YAML, XML has at least the following four important characteristics.

Attributes and Namespaces

  • Metadata can be added to the XML data, just like the id attribute above. Data is held in elements, such as the name of a book author, and metadata (the data of the data) should be held as attributes, which greatly facilitates organizing and structuring the information. Most importantly, both elements and attributes can be marked as belonging to a namespace, which is particularly advantageous when multiple applications use the same XML document.

XML Schema

  • Imagine this situation, when you create an XML document on a machine, on Modifications were made several times on other computers and then transferred to other computers for use, so it was necessary to ensure that the document structure was not destroyed by intermediate operations. For example, one person may use to save the publication date, but another person may use in ISO-8601 format. In order to avoid such structural confusion, we can create a description document XML Schema and save it together with the main document. Before each operation on the main document, its correctness needs to be checked through the schema file. This is a kind of integration test during the production process. RelaxNG has a similar mechanism, but it is much simpler. If you think XML Schema is too complicated, you may try using RelaxNG.

XSL

  • In fact, the modification of XML documents can be completed without any Java/Ruby code. Simply put, you just create an XSL transformation document and apply it to the original XML and get a new XML. The XSL language (Purely Functional Language) is designed for hierarchical data manipulation and is better suited for this task than Java or any other object-oriented/procedural language. With XSL you can transform XML into any form, including plain text and HTML. Many people complain that XSL is too complicated, but it is not that difficult. The core functions of XSL are actually very simple, so you might as well give it a try. The above are not all the characteristics of XML, but these four characteristics are indeed very useful. Not only can they make the document "self-sufficient", they can also perform self-validation (XML Schema), then know how to modify it (XSL), and finally they can easily obtain the document content (Xpath).

    At the same time, there are many languages, standards and applications developed based on XML on the market, including Xforms, SVG, MathML, RDF, OWL, WSDL, etc. But they are not used in general mainstream projects because they are too targeted.

    The purpose of designing JSON is not to meet the above characteristics. Although the JSON field is currently trying hard, including JSONPath for query, some conversion tools and json-schema for validation, compared with the powerful XML, this It's just a trivial imitation. I don't think it will have long-term development, and it will disappear sooner or later.

    To sum up, JSON is a simple data format without too many additional functions. Its best use case is under AJAX. Otherwise, I strongly recommend everyone to use XML.

    OneAPM provides you with end-to-end Java application performance solutions. We support all common Java frameworks and application servers, helping you quickly discover system bottlenecks and locate the root cause of abnormalities. Deploy in minutes and experience it instantly, Java monitoring has never been easier. To read more technical articles, please visit the OneAPM official technical blog.

The above is the detailed content of JSON and XML-detailed explanation of how they are different. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template