Differences between XML and JSON

Source: Internet
Author: User
Tags php server
When I went to the interview this time, my manager asked me the difference between xml and json. I didn't answer the question, so I came back and checked the relevant materials to write something: 1. definition (1 ). XML defines ExtensibleMarkupLanguage (XML), which is used to mark electronic files so that they have a structured markup language. it can be used to mark data and define data types, is a source language that allows you to define your own markup language. Use DTD (documenttypedefiniti... when I went to the interview this time, my manager asked me about the differences between xml and json. I didn't answer the question and felt very sad. so I came back and checked the relevant materials to write something ::

1. definition introduction
(1 ). XML defines Extensible Markup Language (XML), which is used to mark electronic files so that they have a structured Markup Language. it can be used to mark data and define data types, is a source language that allows you to define your own markup language. XML uses the document type definition (DTD) document type definition to organize data. the format is uniform, cross-platform and language, and has long been recognized as a standard in the industry. XML is a subset of the standard general Markup Language (SGML) and is suitable for Web transmission. XML provides a unified way to describe and exchange structured data independent of applications or vendors.
(2) JSON-defined JSON (JavaScript Object Notation) is a lightweight data exchange format with good readability and ease of writing. Data exchange can be performed between different platforms. JSON is highly compatible and completely independent from the language text format. it also has a habit similar to C language (including C, C ++, C #, Java, JavaScript, Perl, Python, etc) system behavior. These features make JSON an ideal data exchange language. JSON is based on a subset of JavaScript Programming Language, 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. Uniform format, compliant with standards;
B. It is easy to remotely interact with other systems and facilitate data sharing.

<2> disadvantages of. XML
A. XML files are large, complex file formats, and bandwidth occupied by transmission;
B. Both the server side and the client side require a large amount of code to parse XML, resulting in abnormal and complex server and client code and difficulty in maintenance;
C. The XML parsing methods for different browsers on the client are inconsistent, and a lot of code needs to be repeated;
D. It takes a lot of resources and time for the server and client to parse XML.

(2) advantages and disadvantages of. JSON

<1> Advantages of. JSON:
A. The data format is relatively simple and easy to read and write. the format is compressed and the bandwidth usage is small;
B. easy to parse. the client 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 for server-side parsing;
D. in the PHP world, there has been a PHP-JSON and JSON-PHP appeared, partial to the PHP serialized program directly called, PHP server-side objects, arrays, etc. can directly generate JSON format, facilitate client access extraction;
E. because the JSON format can be used directly for server-side code, it greatly simplifies the development of server-side and client-side code, and completes tasks unchanged and is easy to maintain.

<2> disadvantages of. JSON
A. The XML format is not widely used and widely used. it is not so universal as XML;
B. The JSON format is still in its infancy in Web Service.

3. Comparison of XML and JSON advantages and disadvantages

(1). readability. JSON and XML have the same data readability. JSON and XML have the same readability. one side is the suggested syntax, and the other side is the standard tag format. XML is more readable.
(2). scalability. XML is inherently highly scalable, and JSON is also available. there is nothing XML can be extended, and JSON cannot.
(3) encoding difficulty. XML has rich coding tools, such as Dom4j and JDom, and JSON also provides tools provided by json.org. However, JSON encoding is much easier than XML, JSON code can be written without tools, but it is not easy to write XML.
(4) decoding difficulty. XML parsing takes into account the parent node of the child node, which is confusing, and the difficulty of JSON parsing is almost 0. There is nothing to say about XML.
(5) popularity. XML has been widely used in the industry, and JSON is just the beginning. However, in the specific field of Ajax, the future development of XML must be in JSON. Then Ajax should be changed to Ajaj (Asynchronous Javascript and JSON.
(6) resolution means. JSON and XML have rich parsing methods.
(7). data volume. JSON is smaller than XML and delivers faster data.
(8) data interaction. The interaction between JSON and JavaScript is more convenient, easier to parse and process, and better data interaction.
(9) data description. JSON is less descriptive than XML.
(10). transmission speed. JSON is much faster than XML.

4. Comparison of XML and JSON data formats
(1). For lightweight and heavyweight, which of the following is the significance of XML to JSON? It should be reflected in parsing. XML currently has two parsing methods: DOM and SAX. <1>. DOMDOM regards a data exchange format XML as a DOM object and needs to read the XML file into the memory. in this regard, the principles of JSON and XML are the same, however, XML should take into account the parent and child nodes. JSON parsing is much less difficult, because JSON is constructed in two structures: key/value, a set of key-value pairs; an ordered set of values, which can be understood as an array;

<2> SAXSAX is a step-by-step Parsing method that can process parsed content without reading the entire document. The program can terminate the resolution at any time. In this way, a large document can be gradually presented at 1.1 points, so SAX is suitable for large-scale parsing. JSON cannot be used at present. Therefore, the lightweight/heavyweight difference between JSON and XML is that JSON only provides the overall resolution scheme, and this method can only achieve good results when parsing less data; XML provides a step-by-step resolution solution for large-scale data, which is suitable for processing large amounts of data.

(2). Data Format Encoding and parsing difficulty

<1>. encoding. Although both XML and JSON have their own encoding tools, JSON encoding is simpler than XML. even if you do not use the tools, you can write JSON code, however, it is a little difficult to write good XML code. JSON, like XML, is also text-based and uses Unicode encoding, and is as readable as XML in data exchange format. Subjectively, JSON is clearer and redundant. The JSON website provides a strict description of the JSON syntax, but the description is short. In general, XML is more suitable for marking documents, while JSON is more suitable for data exchange processing.

<2>. resolution. In the common web application field, developers often worry about XML parsing. whether the server generates or processes XML, or the client uses JavaScript to parse XML, it often leads to complicated code, extremely low development efficiency. In fact, for most Web applications, they do not need complicated XML to transmit data at all. XML claims that scalability has few advantages 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 at the same time lacks some flexibility. Compared with XML or HTML fragments, the data exchange format JSON provides better simplicity and flexibility. In Web Serivice applications, XML remains unshakable for the moment.
(3). XML and JSON comparison instances use structured methods to mark data. The following is a simple comparison.

[{"Length": "12", "url": "http: \/127.0.0.1 \/resources \/videos \/minion_01.mp4", "image": "http: \/127.0.0.1 \/resources \/images \/minion_01.png "," ID ":" 01 "," name ":" Yellow Man division 01st "}, {"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 ":" Porn 02nd "}, {"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 ":" Porn 03rd "}, {"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 ":" Yellow Man 04th "}, {"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 ":" Porn 05th "}, {"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 ":" Yellow Man division 06th "}, {"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 ":" Porn 07th "}, {"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 ":" Yellow Man division 08th "}, {"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 ":" Yellow Man 09th "}, {"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 ":" Yellow Man 10th "}, {"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 ":" Porn 11th "}, {"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 ":" Yellow Man 12th "}, {"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 ":" Yellow Man 13th "}, {"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 ":" Yellow Man 14th "}, {"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 ":" Yellow Man 15th "}, {"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 ":" Yellow Man 16th "}]

XML

 
 
      
      
      
      
      
      
      
      
      
      
      
      
      
      
      
  
 

For the readability of encoding, XML has obvious advantages. after all, human languages are closer to such descriptive structures. JSON reads are more like a data block, which is hard to read. However, the obscure language we read is suitable for machine reading, so the value of "Heilongjiang" can be read through the JSON Index country. provinces [0]. name. For the difficulty of coding handwriting, XML is still more comfortable. if you are good at reading, you can write well. However, the number of characters written in JSON is much less. Without blank tabulation and line breaks, JSON is the useful data, while XML contains many repeated markup characters.

The preceding section details the differences between XML and JSON. 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.