JSON and XML-explanation of different languages

Source: Internet
Author: User
[Editor's note] YegorBugayenko, co-founder of Teamed. io, has a deep research in the field of software quality and engineering management methods. In this article, the author compares JSON to describe the four features of XML in more detail to help you select when setting up a project... [Editor's note] Yegor Bugayenko, co-founder of Teamed. io, has a deep research in the field of software quality and engineering management methods. In this article, the author compares JSON to describe the four features of XML in more detail to help you select the appropriate data format type when setting up a project.

Many people are struggling. if JSON is better than XML, who is faster? Which one will be selected in the next new project? Don't be silly! It is completely incomparable. Like bicycles and AMG S65, which one can you tell? Although both are vehicles, bicycles are more convenient in some cases. So JSON and XML are the same. they both have their own strengths and there is no need to compare them.

Here is a simple JSON data (140 characters ):

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

The same data in XML is expressed as follows (167 characters ):

 
   
  Object Thinking  David West  
      
   
    Microsoft Press
       
   
    2004
     
  
 

It is easy to see the difference, the former is more concise and easier to understand, and can be perfectly parsed in JavaScript. So, can we simply discard the XML choice of JSON? Who will need a 15-year-old heavyweight language?

But on the contrary, I love XML very much and listen to the following decomposition.

But do not misunderstand. This article is not against JSON. JSON is indeed A good data format, but it is only A data format and is often used temporarily to transmit data from point A to Point B. It is simpler and easier to read than XML, but that's all.

XML is a very powerful language, not simply a data format. Compared with JSON and other simple data formats, such as YAML and XML have at least four important features.

  • XPath

To get a similar publication year from the document, you only need to send a simple XPath request:/book/published/year/text (). However, there must be an XPath processor that parses the request and returns 2004. Fortunately, XPath2.0 is a powerful retrieval engine integrating functions, predicates, and axes. without Java code writing any traversal logic, you can add any logic in the form of a natural language in the XPath request. for example, you can directly send the request "How many books were published by David West in 2004?" You can get the answer through XPath. This is what JSON cannot do.

  • Attributes and Namespaces

You can add metadata to XML data, just like the id attribute above. The data is stored in the element, the name of the author of the example book, and metadata should be saved as an attribute, which greatly facilitates organization and structural information. Most importantly, elements and attributes can both be marked as belonging to a namespaces. when multiple applications use the same XML document, this technology has obvious advantages.

  • XML Schema

Think about this situation. when you create an XML document on a machine, make several modifications on another computer, and then upload it to another computer for use, therefore, make sure that the document structure is not damaged by intermediate operations. For example, someone may use Save the publication date, but someone else may use the ISO-8601 format . To avoid such structure confusion, we can create an XML Schema for the description document and save it with the main document. Before each operation on the main document, you must use the schema file to check its correctness. this is an integration test in the production process. RelaxNG is also a similar mechanism, but it will be much simpler. if you think the XML Schema is too complex, try RelaxNG.

  • XSL

In fact, you can modify XML documents without any Java/Ruby code. Simply put, you only need to create an XSL transformation document and apply it to the original XML to get a new XML. The XSL language (pure functional language) is designed for hierarchical data operations. it is more suitable for this task than Java or any other object-oriented/procedural language. XSL can be used to convert XML into any form, including plain text and HTML. Many people complain that XSL is too complex, but it is not that difficult. The core function of XSL is actually very simple. you may try it.

The above is not all the features of XML, but these four features are indeed very useful. They not only make the document "self-sufficiency", but also perform self-validation (XML Schema) to learn how to modify (XSL ), finally, you can easily obtain the document content (Xpath ).

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

The purpose of JSON design is not to meet the above requirements. despite the efforts in the JSON field, including JSONPath for query, some conversion tools, and json-schema for verification, however, compared with powerful XML, this is only a negligible imitation. I don't think it will take a long time and it will disappear sooner or later.

To sum up, JSON is a simple data format without many additional functions. the best use case is AJAX. In addition, I strongly recommend that you use XML.

OneAPM provides you with an end-to-end Java application performance solution. we support all common Java frameworks and application servers to help you quickly discover system bottlenecks and locate the root cause of exceptions. Deployment in minutes, instant experience, Java monitoring has never been so simple. For more technical articles, visit the OneAPM official technical blog.

The above is a detailed explanation of JSON and XML-different languages. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.