XML

Tags: websites, webdevelopment.
By lucb1e on 2011-08-22 23:46:22 +0100

For those who haven't seen XML before, this is what it looks like:
<?xml version and crap>

<users>
    <user>
        <name>lucb1e</name>
        <age>18</age>
        <computer>
            <gpu>nVidia GeForce 9800 GT</gpu>
            <cpu>Intel Core2Quad Q9400</cpu>
        </computer>
    </user>
    <user>
        <name>
et cetera

The first time you see this, it probably looks pretty neat. It is a very clear and usable format. You can easily store users and properties and everything!

Well, appearances are deceptive.

Let me show you an alternative to this that is native Javascript code and can also be natively parsed by PHP:
[

    {
        "name": "lucb1e",
        "age": 18,
        "computer": {
            "CPU": 'Intel Core2Quad Q9400',
            "GPU": 'nVidia GeForce 9800 GT'
        }
    }
]

It may look a bit more cryptic with more symbols instead of words, but the XML is 60% longer! Look closely at the difference between XML and this format (actually, it's called JSON), you'll why it takes so much more space.
XML: <user> data </user>
JSON: "user": data

XML has the word "user" in there twice for no good reason. And see how it needs two bytes (< and >) to open a tag, then three (<, / and >) to close it?
But now what if you have a fieldname called >" ?

XML: <&gt;"> data </&gt;">
JSON: ">\"": data

In every way, JSON is shorter. And if you try to write a program that can read both types of data, you'll notice that JSON works way more easily than XML. You can spend days over all the little problems you'll encounter parsing XML, whereas JSON is incredibly easy.

Also there just is no reason to use XML over JSON when you are simply looking to store or transfer data. Displaying it is a different matter, JSON doesn't exist for very long and browser builders chose XML as the markup language long ago.


So to sum up:
- JSON is shorter (saves disk space)
- JSON is quicker to process (saves CPU)
- XML is better known. You might even mention it to a non-tech person and they have some vague impression what you mean. "Something with code, isn't it?"

Now you might think that this are all subtle differences, and it's a bit like the Windows versus Linux debate. Linux may or may not be better, you may think, but Windows is simply the market standard and thus we use that.

Well no, it's not like that. Neither format is very hard to understand, so there is no learning time involved. In fact, XML has way more insidious tricks than JSON. Also there are plenty of libraries available for both, so you don't have to waste time on that either.

Someone in class ran a website with XML. After switching to JSON the site was 30% faster, which is so much that users really noticed the improvement and asked him about it. And I think I recall someone else also mentioning a rough 1/3rd speed increase after switching to JSON, but I'm not sure where that was.


Conclusion: Save some trees and don't ever use XML again.
lucb1e.com
Another post tagged 'websites': Mind what you post

Look for more posts tagged webdevelopment or websites.

Previous post - Next post