XML vs JSON in AJAX

By: Emiley J. Emailed: 1643 times Printed: 2111 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

If you are working with AJAX, you probably use either one of them or both of them. However there are pros and cons of using each of them. One major drawback of using XML is the speed. XML requires two tags for every data point. These additional data in transmission between the server and the client slows down the data exchange and wastes bandwidth.

In JSON (Java Script Object Notation) objects are small compared to XML documents and working with them is more memory-efficient. While constructing an XML the document may be rather too big just to pass a few values back to the client. This long document takes more memory, The easier method to make a more compact document and to easily parse is provided by JSON.

The other great benefit of JSON is the parsing method with JavaScript's eval() function. You don't need other libraries to parse the JSON document and hence you do not have to worry about cross-browser functionality as you would do in XML parsing. As long as the client browser supports JavaScript the eval() function can be used to parse JSON and interpret data.

So in most cases JSON is a better choice if you the option to choose between XML and JSON.

An example of a JSON data object:

{"conversion":{
"decimal": "120",
"hexadecimal": "78",
"octal": "170",
"binary": "1111000B"}
}

An example of an XML document to store the same data object as above:

<conversion>
<decimal>120</decimal>
<hexadecimal>78</hexadecimal>
<octal>170</octal>
<binary>1111000B</binary>
</conversion>

Ajax Home | All Ajax Tutorials | Latest Ajax Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

Yes, you are right. But JSON is more dangerous that XML in many cases specially when the data comes from an untrusted site. Using the eval() function is an easy way to parse JSON data but it also executes JavaScript commands allowing an attacker to easily execute malicious javascript code.

View Tutorial          By: Nazmun Sadat Khan at 2009-01-29 21:48:26

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J.
Error generating R.java from manifest
ERROR 1251: Client does not support authentication protocol requested by server; consider upgrading MySQL client
IIS and Tomcat - how to configure to work together
PHP Warning: Unknown(): Unable to load dynamic library '/usr/local/php4/lib/php/extensions/no-debug ......
Adding your own Application icon for your J2ME application (jar file)
Creating Menus in Code using VB.net
Traversing all files in a directory using Ruby
Using Proxy to connect to URLs in Ruby
POST a form in Ruby
Reading URL content using Ruby (HTTP)
dRuby client/server mode sample program
Benchmark module in Ruby
encoding and decoding in Ruby using Base64 Module
Module Abbrev in Ruby
Standard Library Packages in Ruby

More Tutorials in Ajax
How to Make an HTTP Request in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
The History of AJAX
To use or not to use AJAX

More Latest News
Most Viewed Articles (in Ajax )
To use or not to use AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
How to Make an HTTP Request in AJAX
Handling the Server Response in AJAX
A complete sample program in AJAX
The History of AJAX
Most Emailed Articles (in Ajax)
A complete sample program in AJAX
The History of AJAX
To use or not to use AJAX
XML vs JSON in AJAX
Creating JSON objects using Java in AJAX
Handling the Server Response in AJAX
How to Make an HTTP Request in AJAX