12

Recently, many programmers and that includes me, have taken the X out of AJAX, and by default use JSON as the protocol format. However I'm trying to think of some places where XML would be more appropriate as a protocol format, that doesn't include SOAP (because SOAP could theoretically be done with JSON anyways).

Note: I love me XML for many other purposes, so its not about XML vs JSON in general, I'm concerned in particular with AJAX's transmission protocol.

Robert Gould
  • 68,773
  • 61
  • 187
  • 272

5 Answers5

15

This question is very similar to When to prefer JSON over XML?

Anyhow, I agree with the top voted answer there:

I use JSON unless I'm required to use XML. It's simpler to understand, and (because it requires less configuration overhead) easier to program for reading and writing if the libraries are available in your context, and they're pretty ubiquitous now.

When Amazon first exposed their catalogs as a web service, they offered both JSON and XML. Something like 90% of the implementers chose JSON.

On the flip side of things, XML is good for situations in which...

  • You need message validation
  • You're using XSLT
  • Your messages include a lot of marked-up text
  • You need to interoperate with environments that don't support JSON
  • You need attributes or namespacing

This is also taken from the question above, which is essentially asking the opposite of this.

Community
  • 1
  • 1
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436
4

For the sake of keeping stuff together, for easier lookup, instead of normalizing the data, I'm regurgitating some parts of the answers on the other question pointed out by Paolo Bergantino, that show benefits of XML:

Favor XML over JSON when any of these is true:

  • You need message validation
  • You're using XSLT Your messages include a lot of marked-up text
  • You need to interoperate with environments that don't support JSON

-Robert Rossney

You need to process the data on the client, and you can leverage XSL for that. Chances are the XML + XSL chain will work faster than JSON + JavaScript especially for big chunks of data. One good case is to convert the data into an HTML snippet.

-Eugene Lazutkin

I'd choose XML over JSON if I need to validate the chunk of incoming data, because XML nativly supports this through XSD.

-lowglider

However JSON is missing both

  • attributes
  • namespacing

-null

Community
  • 1
  • 1
Robert Gould
  • 68,773
  • 61
  • 187
  • 272
2

Ajaxian wrote about this very topic - JSON vs. XML: The Debate

Additionally, and I'm not sure of the importance, but some instances might call for attributes on tags, and you can't really place attributes on JSON-entries. I could be way off there though - JSON/XML aren't my strongest areas.

Sampson
  • 265,109
  • 74
  • 539
  • 565
  • I didn't down vote you, however my guess is that your giving an "obvious" example in favor of JSON. When this question is about stuff in favor of XML? – Robert Gould Jan 26 '09 at 05:04
  • @Robert - you're right. I've modified to post to be more appropriate. – Sampson Jan 26 '09 at 05:08
1

If you were going to do XSLT conversions on the client side.

Zach
  • 24,496
  • 9
  • 43
  • 50
0

For me, I think the biggest drawback of using XML in AJAX is the fact that you will usually have to parse it and convert it in some way, whereas you won't have to do the same with JSON because the format is native to JavaScript. When you add the larger transmission cost to the parsing and conversion, I honestly can't think of a good reason to use XML over JSON for AJAX, though I certainly use XML for many other things.

VirtuosiMedia
  • 52,016
  • 21
  • 93
  • 140
  • Eh? var doc = req.responseXML. Then use DOM functions on doc. – Zach Jan 26 '09 at 04:53
  • JSON is not native to javascript - you would still need to parse it. IT just seem like you didn't have to because the browser automatically did it if you loaded it in a – Chii Jan 26 '09 at 05:49