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

Detailed introduction to the difference between XML and JSON

May 07, 2017 pm 01:49 PM

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

[

  {

    "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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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

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.

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.

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

See all articles