XML? You Gotta be Kiddin…
Posted by Jonathan Ng | Filed under Technical
If you’re tried doin your own Ajax app, no matter the size, you’ll probably know for a fact that parsing XML with JavaScript is no easy feat. Heck, the word “parsing” doesn’t even make it sound easy. Most of us would resort to setting things up in such a way that the server returns (X)HTML codes that are slapped into the DOM tree. For small “applications”, like my explore box, using some overweight library just to parse XML for display is foolish. Small “applications” should of course, feel small. They should load quickly and respoind instaneously.
So introducing: JSON (JavaScrip Object Notation). Simply put, JSON is yet another text-based way of representing data (like XML), but it can be parsed much more easily. How easily? For JavaScript, zero libraries are needed. How easily? Say you have an obejct defined in aJSONtext in JSON notation, all you need is: var myObject = eval('(' + aJSONtext + ')');. And there you have a JavaScript object with the necessary data. More on JSON in JavaScript here.
Unfortunately, XML is not well suited to data-interchange, much as a wrench is not well-suited to driving nails. It carries a lot of baggage, and it doesn’t match the data model of most programming languages. When most programmers saw XML for the first time, they were shocked at how ugly and inefficient it was. It turns out that that first reaction was the correct one. There is another text notation that has all of the advantages of XML, but is much better suited to data-interchange. That notation is JavaScript Object Notation (JSON).
You can read about JSON versus here at it’s official site. JSON’s (I don’t know if it’s “J-S-O-N” or simply “Jason”) been around for a while now. But there’s a number of recent articles tat you should read: JSON versus XML, speeding up AJAX with JSON and XMLHTTPRequest versus iFrames.
The article on iFrames is pretty interesting. You could probably conclude that using iFrames + JavaScript (JSON) is the better compared to AJAX.