Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data efficiency - return in JSON or XML?

I have fairly large set of data returned via AJAX from a page. This can be anything up to 0-20k records at once, each with around 10 peices of data inside. Now, at the moment, the data is returned in structured XML and the javascript deals with it (for the record, I'm using jQuery at the moment).

When the XML comes back, the jQuery loops through all the nodes called address (using the .find() function). It then sorts the data respectively and can take any amount of time. However it does appear the code isn't as efficient as maybe it could be, and I don't really have any experience handling largish data. I guess the benefit of the data being XML is we can easily integrate it into other things, eg Web services etc... but efficiency would be much better as we dont want to leave the user "hanging". So I'm sure other people may have asked this before, but what's more efficient - XML or JSON?

Also, which one is more useful, or are they both the same?

Sorry if it's a lame question, this is one of the first times I've used JSON!

like image 887
rickyduck Avatar asked Jan 19 '23 11:01

rickyduck


1 Answers

JSON is a notation for serializing objects. That's its focus and purpose. XML is for defining markup languages, but I don't like it if people hammer that fact home as if it can't be used for data. I find XML very suitable for representing a multitude of data structures. However, XML is rather general-purpose and that brings some overhead with it in terms of specification, notation and tool size.

First of all, find out exactly what you need. Do you need to transfer data in a more general sense, or is passing around objects sufficient? Will you require things like namespaces? Choose a technology that does what you need, but not more than that. But do keep future expansion in mind.

Secondly, consider the tools. XML has great support in almost any language. There's methods for in-memory representation (DOM), object binding (JAXB in Java), parsing (SAX)... Does JSON have as much support in your target environment? On the other hand, JSON is suppremely convenient on the client-side in combination with JavaScript.

I believe you'll be able to do what you need regardless of technology choice, and within each choice there's room for optimization. But there's one final thing to consider: maybe you don't have to choose. Sometimes it can be really simple to allow data to be serialized as JSON or XML. Being a Java programmer, this is the only example I can come up with, but in JAX-WS there are methods for getting data from webservices as XML, JSON or maybe even other formats with minimal code adaptation.

like image 73
G_H Avatar answered Jan 30 '23 21:01

G_H