XML and JSON comparisons

Source: Internet
Author: User

  At present, in the field of web development, the main data Interchange format has XML and JSON, for XML Trust every web developer is not unfamiliar; In contrast, JSON may be unfamiliar to some new newcomers to the development world, or you may have heard of it before, But the differences between XML and JSON may not be well understood. There has always been a controversy over the choice of XML or JSON in AJAX development, although the JSON is in the early stages, but I believe JSON will eventually replace XML as the Ajax choice, and Ajax may be renamed Ajaj ( Asynchronous JavaScript and JSON);  1. Data Interchange Format comparison of XML and Json: xml:extensible Markup Language, an HTML-like language, He has no pre-defined tags, uses the DTD (document type definition) to organize the data, the format is unified, cross-platform and language, has already become the industry accepted standard. Specifically, you can ask Google or Baidu. In contrast to this lightweight data interchange format of JSON, XML can be called Heavyweight. The  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.  2. Data Interchange Format comparison of lightweight and heavyweight:  lightweight and heavyweight is relative, so what is the weight level of XML relative to JSON? I think it should be reflected in the analysis, XML is currently designed two types of parsing: Dom and Sax; dom is a data Interchange Format XML as a DOM object, the XML file needs to be read into memory, which the principle of JSON and XML is the same, However, XML considers the parent and child nodes, which is much less difficult to parse because JSON is built on two structures: Key/value, a set of key-value pairs, and an ordered set of values that can be understood as an array; sax does not need to read the entire document can be parsed out of the content of processing, is a step-by-phase analysis method. The program can also terminate parsing at any time. In this way, a large document can be gradually and 1.1 points, so sax is suitable for large-scale parsing. At this point, JSON is not available at this time.   Therefore, the light/heavyweight difference between JSON and XML is that JSON provides only a holistic parsing scheme that works well when parsing less data, while XML provides a step-by-stage solution to large-scale data that is well suited for processing large amounts of data.  3. Data Interchange Format comparison of data format encoding and parsing difficulty:  on the encoding, although both XML and JSON have their own coding tools, JSON encoding is simpler than XML, and even without tools, you can write JSON code. But it's a little difficult to write good XML code; Like XML, JSON is text-based, and they all use Unicode encoding and are as readable as data Interchange Format XML. In   view, JSON is clearer and less redundant. The JSON Web site provides a strict description of the JSON syntax, just a short description. In general, XML is more appropriate for tagging documents, while JSON is more suitable for data interchange processing.   In the analytic, in the common Web application domain, the developer often has the nerve to parse the XML, whether the server side generates or processes the XML, or the client uses the JavaScript parsing XML, often causes the complex code, extremely low development efficiency.   In fact, for most Web applications, they don't need complex XML to transmit data at all, and the extensibility of XML claims is rarely an advantage; many AJAX applications even return HTML fragments directly to build dynamic Web pages. Returning HTML fragments greatly reduces the complexity of the system compared to returning XML and parsing it, but at the same time lacks some flexibility. The Data Interchange Format JSON provides greater simplicity and flexibility than XML or HTML fragments. In Web Serivice applications, at least for now, XML still has an unshakable position.  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 language similar to the C family, including C,C + +, C #, Java, Javascript, Perl, Python, etc.). These features make JSON an ideal data exchange language. The  json concept is simple, that is, the server directly generates JavaScript statements, the client obtains directly with the Eval method to obtain this object, so as to eliminate the loss of parsing XML.    For example,:   uses XML to represent:  <items> <item> <id>1</id>  <author>asp</author> <url>http://www.aspxuexi.com</url> <content >welcome to Aspxuexi.com</content> </item> <item> <id>2</id>  <author>relkn</author> <url>http://www.aspxuexi.com</url> <content >aspxuexi.com Focus on asp</content> </item> <item> <id>3</id>  <author>kak</author> <url>http://www.aspxuexi.com</url> <content> www.aspxuexi.com ASP Tutorials </content> </item> </items>  <items>  <item> <id>1</id> <author>aspxuexi</author> <url>http://Www.aspxuexi.com</url> <content>Welcome to Aspxuexi.com</content> </item>  <item> <id>2</id> <author>relkn</author> <url>http:// Www.aspxuexi.com</url> <content>aspxuexi.com focus on internet technology </content> </item>  <item> <id>3</id> <author>kvogend</author> <url>http:// Www.aspxuexi.com</url> <content>aspxuexi.com Focus on web2.0</content> </item>  </items>  using Json: {items:[ { id:1, author:\ "aspxuexi\",  url:\ "/http Www.aspxuexi.com\ ",  content:\" Welcome to Aspxuexi.com\ " }, { id:2, author:\" Relkn\ ",  url:\ "http://www.aspxuexi.com\",  content:\ "aspxuexi.com pay attention to new technology of internet"  }, { id:3,  Author:\ "kvogend\",  url:\ "http://www.aspxuexi.com\",  content:\ "aspxuexi.com concern Web2.0\" &NBSP;}&NBSP;]} ;   {items:[  { id:1, author:\ "aspxuexi\",  url:\ "http://www.aspxuexi.com\",  content:\ "Welcome to Aspxuexi.com\ " }, { id:2, author:\" relkn\ ",  url:\" http://www.aspxuexi.com\ ",  Content:\ "aspxuexi.com focus on internet new technology \"  }, { id:3, author:\ "kvogend\",  url:\ "/http Www.aspxuexi.com\ ",  content:\" aspxuexi.com focus on Web2.0\ " } ]}; json not only reduces the performance and compatibility issues associated with parsing XML parsing, It is also very easy to use for JavaScript, it is easy to iterate through the array and access the object properties to get the data, its readability is good, basically with the nature of structured data. It's a good idea to say, and in fact, Google Maps does not use XML to pass data, but instead uses the JSON scheme. Another advantage of   json is "cross-domain", such as the use of  <script type= "Text/javascript" src= "in the Www.aspxuexi.com Web page" http// Www.yyy.com/some.js ">  is perfectly feasible, which means you can pass information across domains. The use of XMLHttpRequest does not obtain cross-domain information, which is limited by the security nature of JavaScript. Can   json completely replace XML? Of course not, the reason is the advantages of XML: versatility. It is not easy to make syntactically qualified JavaScript code on the server side, which occurs mainly in larger systems where the server side and the client have different developers. They must negotiate the format of the object, which can easily cause errors.  ajax: Embracing JSON, letting XML walk away    Ajax (asynchronous Javascript and XML) is ultimately a technique for browsers to read XML content on a server asynchronously. Now the technology whereIs the relationship with the XML, coupled with a concept to do the pretence, like a gold-loaded, not dragged. The man outside the door was very lively, and the man in the door shook his head. What about XML, a cross-platform, trendy language? Actually xml=txt. XML is just text that conforms to many specifications. It itself is nothing but a file that saves characters. The browser asynchronously reads only the text content on the server, so there is no need to stick to XML when developing AJAX. The origin of   JSON    the function of XML is to format the data content. What better way to do it if we don't use XML? The answer is JSON. Before I introduce JSON, let me introduce the scripting language of JavaScript. The scripting language itself has the gift of dynamic execution. That is, we can put the statement that we want to execute in a string and execute it through the dynamic execution function of Eval_r (). The contents of the string are executed just like the script we wrote.    example 1:  <HTML>   <HEAD>   <TITLE> Eval example 1 </TITLE>   </HEAD>   <BODY>    <script>   str = "alert (' Hello ')";  eval_r (str);  </script>   </BODY>   </HTML>   Opening the page will pop up the Hello window.    we can put any script statement in the string, including the declaration statement:   <HTML>   <HEAD>   <TITLE> Eval Example 2 </TITLE>   </HEAD>   <BODY>   <script>   define = "{name: ' Michael ', email: ' [email protected] '}";  eval_r ("data =" + Define);  alert ("Name:" +data.name);  alert ("Email:" +data.email);   </script>   </BODY>  </ html>  If the text that we came to asynchronously in the background is a JavaScript statement, then not an eval method can parse it? How much more efficient it is to parse complex XML?!    now to tell you what is Json:javascript Object Notation. I'd prefer to translate it into a JavaScript object declaration. For example, to load some directory information from the background, if written as XML, as follows:   <contact>   <friend>   <name> Michael </name>   <email> [email   Protected] </email>  

XML and JSON comparison

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.