XML vs. JSON

Source: Internet
Author: User

XML vs. JSON 1, each defined

Xml

The Extensible Markup Language (extensible Markup Language, XML), which is used to tag electronic files with a structured markup language that can be used to tag data, define data types, is a source language that allows users to define their own markup language. XML uses the DTD (document type definition) document type definition to organize the data, a unified format, cross-platform and language, and has long been recognized as a standard in the industry. XML is a subset of standard Generalized Markup Language (SGML) and is ideal for Web transport. XML provides a unified approach to describing and exchanging structured data that is independent of the application or vendor. JSON JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

JSON is constructed in two structures:

A collection of name/value pairs (A collection of name/value pairs). In different languages, it is understood as objects (object), record (record), structure (struct), Dictionary (dictionary), hash table (hash table), keyed list (keyed list), or associative array (associative Array). The sequence of values (an ordered list of values). In most languages, it is understood as an array (array).

More about JSON, visible: http://json.org

2.XML and JSON comparison

Xml

1) wide application, strong scalability, widely used in various occasions 2) read, parse no JSON fast 3) readable, can describe complex structure

The application of XML has been deeply rooted, no longer repeat.

JSON 1) is a simple structure and is a key-value pair. 2) Read, parse fast, many language support 3) small transmission data, transmission rate greatly improved

4) Describe the weak structure of complex structures

Native support for JavaScript, PHP, etc. simplifies read parsing. Become the data structure commonly used in the current Internet era.

3. XML and JSON conversion

The conversion between XML and JSON is often used, where the difference between the two is more clearly understood.

1) General Conversion

Regular node conversions, the following cases can visually see the principle of conversion between each other.

XML is the node name and value, and JSON is the key and value pair of the key and value. Such conversions are the simplest.

Xml:

<Student> Zhang San </Student>

Convert to JSON:

{"Student": "Zhang San"}

The process is reversible, and JSON can be easily converted to XML.

2) XML node contains attributes

Nodes contain attributes, their conversions are different, in JSON, XML attributes are also used as properties of JSON objects, in order to distinguish them from other nodes, the node name here adds a special prefix that identifies "@" to identify the attribute,

Another strange thing is that the value of the node, the property name is added, and there is a special prefix, "#text". This processing allows the conversion of XML and JSON to implement reversible operations. At the beginning of the @ in JSON, when you convert to XML, you need to generate node properties, #text则为节点的内容. The conversion here is a common practice for commonly used converters.

The following conversion examples

Xml:

<ID= "1001"> Zhang San </Student>     

Convert to JSON

{"  Student": {    "@ID": "1001",    "#text": "Zhang San"  }}  

3) node-generated object with attributes

Example 1, no attribute node, simple string array

<line<item>name</Item<item>age</Item  ></line>              

Convert to JSON:

{"Line  ": {"    item": [      "Name",      "age"    ]  }}

Example 2, a property-to-object, without property, directly to a string

have attribute nodes

<line<font-size= ">name"</Item<item>age< /Item></line>          

Convert to JSON:

{"Line  ": {"    item": [      {        "@font-size": "$",        "#text": "Name"      },      "Age"    }  }

4) The node order of XML

XML has a sequence between nodes, but when it is converted to JSON, the order is sometimes disrupted.

This can be explained by the fact that XML is a big difference from JSON, describing the structural order, especially complex structures, when JSON is not as clear as XML.

In the following example, we can see clearly that the conversion is irreversible.

Example:

Xml:

<template> << Span style= "color: #800000;" >block</> <line< Span style= "color: #0000ff;" >> Horizon </line<block> Underground Span style= "color: #0000ff;" ></block> </ template>        

Transform to JSON:

{"  template": {    "block": [      "Sky",      "Underground"    ],    "line": "Horizon" }} 

Here, we can see that in JSON, two blocks are put together, the middle line is placed in the back, the original node order of XML is disrupted.

If you want to convert from JSON to original XML, it is not possible.

If you want to be serious, you can certainly specify specific properties in JSON, identify their order, or the relationship between the nodes, but doing so does add complexity.

4. Xml attributes vs. elements

Property cannot contain multiple values (element can) attribute cannot be described tree structure (element can) attributes are difficult to read and maintain because properties are not easy to extend (for future changes)

The following XML feels stupid:

< time= "2014-10-01 10:10:02" to= "Lucy" from= "Jim" body= "Don ' t forget the meeting! " ></message>           

should be:

<Message><From>jim</from> <to>lucy</to< Span style= "color: #0000ff;" >> <time> 2014-10-01 10:10:02</time > <body>don ' t forget the meeting! </body> </message>      

Principle:

Metadata (data about data) should be stored as attributes, and the data itself should be stored as an element.

5. Recommended Selection principle

1) for complex data structures using XML, a simple data structure can be JSON-based.

2) The use of JSON is advocated in data transmission in Internet applications.

3) for the difference between the front and back, you can use XML on the server side, using JSON at the front end, the conversion between the two is done on the servers. This will take the director.

XML vs. JSON

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.