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

Mar 17, 2017 pm 05:04 PM

[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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I open an XML file using PowerPoint? Can I open an XML file using PowerPoint? Feb 19, 2024 pm 09:06 PM

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Combination of golang WebSocket and JSON: realizing data transmission and parsing Combination of golang WebSocket and JSON: realizing data transmission and parsing Dec 17, 2023 pm 03:06 PM

The combination of golangWebSocket and JSON: realizing data transmission and parsing In modern Web development, real-time data transmission is becoming more and more important. WebSocket is a protocol used to achieve two-way communication. Unlike the traditional HTTP request-response model, WebSocket allows the server to actively push data to the client. JSON (JavaScriptObjectNotation) is a lightweight format for data exchange that is concise and easy to read.

What is the difference between MySQL5.7 and MySQL8.0? What is the difference between MySQL5.7 and MySQL8.0? Feb 19, 2024 am 11:21 AM

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Pandas usage tutorial: Quick start for reading JSON files Pandas usage tutorial: Quick start for reading JSON files Jan 13, 2024 am 10:15 AM

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese Mar 05, 2024 pm 02:48 PM

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

How to use PHP functions to process XML data? How to use PHP functions to process XML data? May 05, 2024 am 09:15 AM

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

See all articles