Home > Backend Development > XML/RSS Tutorial > Detailed introduction to the difference between XML and JSON

Detailed introduction to the difference between XML and JSON

黄舟
Release: 2017-05-07 13:49:25
Original
2677 people have browsed it

I went for an interview today, and a human resources manager asked me the difference between xml and json. I felt sad that I didn’t answer, so I asked me when I came back. Check out relevant information and write something::

1. Definition introduction
(1). XML definition Extensible Markup Language (XML), a mark used to mark electronic documents to make them structural. Language can be used to mark data and define data types. It is a source language that allows users to define their own markup language. XML uses DTD (document type definition) document type definition to organize data; the format is unified, cross-platform and language, and has long become a recognized standard in the industry. XML is a subset of Standard Generalized Markup Language (SGML) and is well suited for Web transport. XML provides a unified method for describing and exchanging structured data independent of applications or vendors.
(2).JSON definition JSON (JavaScript Object Notation) is a lightweight data exchange format that has good readability and is easy to write quickly. Data exchange between different platforms is possible. JSON adopts a highly compatible and completely language-independent text format, and also has behavior similar to C language habits (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These properties make JSON an ideal data exchange language. JSON is based on JavaScript Programming Language, a subset of Standard ECMA-262 3rd Edition - December 1999.
2. Advantages and Disadvantages of XML and JSON
(1). Advantages and Disadvantages of XML

<1>. Advantages of XML
A. The format is unified and conforms to standards;
B .Easily interact with other systems remotely, and data sharing is more convenient.

<2>.Disadvantages of XML
A.XML files are huge, the file format is complex, and transmission takes up bandwidth;
B.Both the server and the client need to spend a lot of code to parse XML, As a result, the server-side and client-side codes become extremely complex and difficult to maintain;
C. The way of parsing XML between different browsers on the client side is inconsistent, and a lot of code needs to be written repeatedly;
D. Server-side and client-side parsing XML consumes more resources and time.

(2). Advantages and disadvantages of JSON

<1>. Advantages of JSON:
A. The data format is relatively simple, easy to read and write, the format is compressed, and takes up Small bandwidth;
B. Easy to parse, client-side JavaScript can simply read JSON data through eval();
C. Supports multiple languages, including ActionScript, C, C#, ColdFusion, Java, JavaScript, Perl, PHP, Python, Ruby and other server-side languages ​​facilitate server-side parsing;
D. In the PHP world, PHP-JSON and JSON-PHP have appeared, favoring PHP sequences The transformed program is directly called, and the object, array, etc. on the PHP server can directly generate JSON format, which is convenient for client access and extraction;
E. Because the JSON format can be directly used by the server The use of end-side code greatly simplifies the amount of code development on the server side and client side, and the tasks remain unchanged and is easy to maintain.

<2>.Disadvantages of JSON
A. It is not as popular and widely used as XML format, and it is not as versatile as XML;
B.JSON format is currently used in Web Service Promotion is still in its infancy.

3. Comparison of the advantages and disadvantages of XML and JSON

(1). Readability. The data readability of JSON and XML is basically the same. The readability of JSON and XML is almost the same. One side has the recommended syntax and the other side has the standardized tag form. XML is more readable.
(2). Scalability. XML is naturally very scalable, and JSON certainly has it. There is nothing that XML can expand that JSON cannot.
(3). Coding difficulty. XML has a wealth of encoding tools, such as Dom4j, JDom, etc., and JSON also has tools provided by json.org. However, JSON encoding is obviously much easier than XML. You can write JSON code even without the help of tools, but it is difficult to write XML well. Not so easy anymore.
(4). Decoding difficulty. The parsing of XML has to consider the child nodes and parent nodes, which makes people dizzy, while the difficulty of parsing JSON is almost 0. XML loses really nothing at this point.
(5). Popularity. XML has been widely used in the industry, and JSON has just begun. However, in the specific field of Ajax, the future development must be that XML gives way to JSON. By then Ajax should become Ajaj (Asynchronous Javascript and JSON).
(6). Analysis methods. JSON and XML also have rich parsing methods.
(7). Data volume. Compared with XML, JSON has a smaller data size and faster transmission speed.
(8).Data interaction. The interaction between JSON and JavaScript is more convenient, easier to parse and process, and provides better data interaction.
(9). Data description. JSON is less descriptive of data than XML.
(10).Transmission speed. JSON is much faster than XML.

4. Comparison of XML and JSON data formats
(1). Regarding lightweight and heavyweight, lightweight and heavyweight are relative terms, so where is the heavyweight of XML compared to JSON? It should be reflected in parsing. XML is currently designed with two parsing methods: DOM and SAX. <1>.DOMDOM regards a data exchange format XML as a DOM object, and the entire XML file needs to be read into the memory. The principles of JSON and XML are the same in this regard, but XML must be considered Parent nodes and child nodes, JSON is much less difficult to parse at this point, because JSON is built on two structures: key/value, a collection of key-value pairs; an ordered collection of values, which can be understood as an array ;

<2>.SAXSAX can process the parsed content without reading the entire document. It is a step-by-step parsing method. The program can also terminate parsing at any time. In this way, a large document can be displayed gradually and bit by bit, so SAX is suitable for large-scale parsing. This is currently not possible with JSON. Therefore, the light/heavyweight difference between JSON and XML is that: JSON only provides an overall parsing solution, and this method can only achieve good results when parsing less data; XML provides a step-by-step parsing of large-scale data scheme, which is very suitable for processing large amounts of data.

(2). Regarding the difficulty of data format encoding and parsing

<1>.In terms of encoding. Although both XML and JSON have their own encoding tools, the encoding of JSON is simpler than that of XML. You can write JSON code even without the help of tools, but it is a bit difficult to write good XML code; like XML, JSON is also Text-based, they all use Unicode encoding and are as readable as the data exchange format XML. Subjectively, JSON is cleaner and less redundant. The JSON website provides a strict, if brief, description of JSON syntax. Generally speaking, XML is more suitable for marking documents, while JSON is more suitable for data exchange processing.

<2>.In terms of analysis. In the field of ordinary web applications, developers often worry about XML parsing. Whether it is generating or processing XML on the server side, or parsing XML on the client side using JavaScript, it often results in complex code and extremely low development efficiency. In fact, for most Web applications, they do not need complex XML to transmit data at all. The extensibility claimed by XML rarely has an advantage here. Many Ajax applications even directly return HTML fragments to build dynamic Web pages. Compared with returning XML and parsing it, returning HTML fragments greatly reduces the complexity of the system, but it also lacks a certain degree of flexibility. The data exchange format JSON provides greater simplicity and flexibility than XML or HTML fragments. In Web Service applications, at least for now, XML still has an unshakable position.
(3). Example comparison XML and JSON both use structured methods to mark data. Let’s do a simple comparison below.

[
  {
    "length" : "12",
    "url" : "http:\/\/127.0.0.1\/
resource
s\/videos\/minion_01.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_01.png",
    "ID" : "01",
    "name" : "小黄人 第01部"
  },
  {
    "length" : "14",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_02.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_02.png",
    "ID" : "02",
    "name" : "小黄人 第02部"
  },
  {
    "length" : "16",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_03.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_03.png",
    "ID" : "03",
    "name" : "小黄人 第03部"
  },
  {
    "length" : "18",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_04.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_04.png",
    "ID" : "04",
    "name" : "小黄人 第04部"
  },
  {
    "length" : "20",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_05.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_05.png",
    "ID" : "05",
    "name" : "小黄人 第05部"
  },
  {
    "length" : "22",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_06.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_06.png",
    "ID" : "06",
    "name" : "小黄人 第06部"
  },
  {
    "length" : "24",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_07.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_07.png",
    "ID" : "07",
    "name" : "小黄人 第07部"
  },
  {
    "length" : "26",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_08.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_08.png",
    "ID" : "08",
    "name" : "小黄人 第08部"
  },
  {
    "length" : "28",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_09.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_09.png",
    "ID" : "09",
    "name" : "小黄人 第09部"
  },
  {
    "length" : "30",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_10.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_10.png",
    "ID" : "10",
    "name" : "小黄人 第10部"
  },
  {
    "length" : "32",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_11.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_11.png",
    "ID" : "11",
    "name" : "小黄人 第11部"
  },
  {
    "length" : "34",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_12.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_12.png",
    "ID" : "12",
    "name" : "小黄人 第12部"
  },
  {
    "length" : "36",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_13.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_13.png",
    "ID" : "13",
    "name" : "小黄人 第13部"
  },
  {
    "length" : "38",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_14.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_14.png",
    "ID" : "14",
    "name" : "小黄人 第14部"
  },
  {
    "length" : "40",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_15.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_15.png",
    "ID" : "15",
    "name" : "小黄人 第15部"
  },
  {
    "length" : "42",
    "url" : "http:\/\/127.0.0.1\/resources\/videos\/minion_16.mp4",
    "image" : "http:\/\/127.0.0.1\/resources\/images\/minion_16.png",
    "ID" : "16",
    "name" : "小黄人 第16部"
  }
]
Copy after login

XML

<?xml version="1.0" encoding="UTF-8"?>
<vedios>
<vedio ID = "01" name = "小黄人 第01部" length = "12" image = "http://127.0.0.1/resources/images/minion_01.png" url = "http://127.0.0.1/resources/videos/minion_01.mp4" />    
<vedio ID = "02" name = "小黄人 第02部" length = "14" image = "http://127.0.0.1/resources/images/minion_02.png" url = "http://127.0.0.1/resources/videos/minion_02.mp4" />    
<vedio ID = "03" name = "小黄人 第03部" length = "16" image = "http://127.0.0.1/resources/images/minion_03.png" url = "http://127.0.0.1/resources/videos/minion_03.mp4" />    
<vedio ID = "04" name = "小黄人 第04部" length = "18" image = "http://127.0.0.1/resources/images/minion_04.png" url = "http://127.0.0.1/resources/videos/minion_04.mp4" />    
<vedio ID = "05" name = "小黄人 第05部" length = "10" image = "http://127.0.0.1/resources/images/minion_05.png" url = "http://127.0.0.1/resources/videos/minion_05.mp4" />    
<vedio ID = "06" name = "小黄人 第06部" length = "12" image = "http://127.0.0.1/resources/images/minion_06.png" url = "http://127.0.0.1/resources/videos/minion_06.mp4" />    
<vedio ID = "07" name = "小黄人 第07部" length = "16" image = "http://127.0.0.1/resources/images/minion_07.png" url = "http://127.0.0.1/resources/videos/minion_07.mp4" />    
<vedio ID = "08" name = "小黄人 第08部" length = "18" image = "http://127.0.0.1/resources/images/minion_08.png" url = "http://127.0.0.1/resources/videos/minion_08.mp4" />    
<vedio ID = "09" name = "小黄人 第09部" length = "20" image = "http://127.0.0.1/resources/images/minion_09.png" url = "http://127.0.0.1/resources/videos/minion_09.mp4" />    
<vedio ID = "10" name = "小黄人 第10部" length = "12" image = "http://127.0.0.1/resources/images/minion_10.png" url = "http://127.0.0.1/resources/videos/minion_10.mp4" />    
<vedio ID = "11" name = "小黄人 第11部" length = "13" image = "http://127.0.0.1/resources/images/minion_11.png" url = "http://127.0.0.1/resources/videos/minion_11.mp4" />    
<vedio ID = "12" name = "小黄人 第12部" length = "12" image = "http://127.0.0.1/resources/images/minion_12.png" url = "http://127.0.0.1/resources/videos/minion_12.mp4" />    
<vedio ID = "13" name = "小黄人 第13部" length = "11" image = "http://127.0.0.1/resources/images/minion_13.png" url = "http://127.0.0.1/resources/videos/minion_13.mp4" />    
<vedio ID = "14" name = "小黄人 第14部" length = "15" image = "http://127.0.0.1/resources/images/minion_14.png" url = "http://127.0.0.1/resources/videos/minion_14.mp4" />    
<vedio ID = "15" name = "小黄人 第15部" length = "14" image = "http://127.0.0.1/resources/images/minion_15.png" url = "http://127.0.0.1/resources/videos/minion_15.mp4" />    
<vedio ID = "16" name = "小黄人 第16部" length = "18" image = "http://127.0.0.1/resources/images/minion_16.png" url = "http://127.0.0.1/resources/videos/minion_16.mp4" /></vedios>
Copy after login

In terms of coding readability, XML has obvious advantages. After all, human language is closer to such a description structure. JSON reads more like a data block and is more confusing to read. However, the language that is difficult for us to read is exactly suitable for machine reading, so the value of "Heilongjiang" can be read through JSON's indexcountry.provinces[0].name . In terms of the handwriting difficulty of coding, XML is more comfortable. It is easy to read and of course easy to write. However, the written JSON characters are obviously much less. If you remove the blank tabs and line breaks, JSON is densely packed with useful data, while XML contains many repeated markup characters.

The above is the detailed content of Detailed introduction to the difference between XML and JSON. 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