sometimes it feels like XML has been used just because it was fashionable.
When and Why is XML preferable to CSV?
86.5k views Asked by Nick AtThere are 20 answers
I found an interesting performance test on the net. God example of drawbacks of XML when the features of XML is not needed.
"I tried Steven's experiment from a different angle. I filled an Excel XP spreadsheet with a single-digit number, saved it in both XML and in a comma-delimited text file (CSV). I then compressed both with WinZip and then opened both with Excel. Here's what I found:
The XML file was 840MB, the CSV 34MB -- a 2,500% difference Compressed, the XML file was 2.5MB, the CSV 0.00015MB (150KB) -- a 1,670% difference.
Equally dramatic is the time it took to uncompress and render the files as an Excel spreadsheet: It took about 20 minutes with the XML file; the CSV took 1 minute -- a 2,000% difference."
XML has become the default for its many benefits that lots of other people have already mentioned. So the question really becomes "When and Why is CSV preferable to XML?".
I feel CSV is preferable to XML when: - you are loading simple tabular data - you are in control of both the generation and consumption of the data file - the dataset is large
CSV is perfectly usable if the first 2 points are true, and has a performance benefit that becomes more significant the larger the dataset is.
I did a quick test loading ~8000 records each with 6 text fields. Loading and parsing the XML took ~8 seconds. Loading the CSV took less than 1 second.
The overhead of XML is worth it in a lot of cases, but when the stars align, CSV makes more sense.
CSV is useful when you just have a series of a values that relate to some piece of information and you know you will always store values for each field.
XML has the benefit of having self-describing data (tags) and having hierarchy - which gives you a lot more flexibility in the way that you store the data.
Of course it is fashionable and buzz-worthy sometimes. It all depends on your application. I prefer config files in XML because they are easy to parse. Whereas, I use CSV files for DataGridView or database dumps.
This Daily WTF : XML vs CSV The Choice is Obvious will help you make your decision ;)
XML provides a way of tagging your data with metadata (provided by the tag names and attribute names), whereas CSV does not. Couple this with the ability to define structured hierarchies and it makes XML easier to understand when provided with just the data, whereas CSV would require an accompanying tool or document to describe how each value is interpreted.
And again one more for XML: The X in XML stands for Extensible (I know, not really mnemonic :-P). That means, with the help of the XML namespace mechanism, you can join any two XML languages you like and combine them in the same document. Given that there is only one CSV 'language' (not counting the myriads of delimiter styles), XML can handle quite a lot of complexity, and that in a modular way.
This however, is the advantage of CSV: If you really have tabular data, XML syntax is most often overkill.
I don't have enough reputation to comment on the relevant answer, but someone suggested compressing the XML as a way to gain size parity with csv formats. While this is true, XML compression can somtimes come back to bite you. If you are transferring XML data from point to point and it fails, it's nice to be able to read the XML and figure out what went wrong. If the XML is compressed and the transfer fails, it's sometimes not possible to decompress it and examine the contents. In other words compressing XML cancels out the human-readability advantage it has.
I have found of the greatest advantages of XML to be the parsing functionality and the strict validation that comes out-of-the-box with most XML libraries. The insistence on well-formedness and easy-to-understand error message (xyz not closed in line x, column y) are a real help compared to hunting broken values, or unknown behaviour, because of an error in the CSV file.
- There are existing parsers and emitters for it in every language and database
- They deal with encoding for me
- They deal with escaping for me
That's all that matters to me.
Sure, there's a semi-standard way to do escaping in CSV (i.e., "the way Excel does it"), and it's not exactly hard to write yourself, but it does take some time. And then you've got to implicitly agree on a character encoding out-of-band. But then, because it's so simple, people try to write it themselves, and invariably screw up either #2 or #3.
JSON also meets #2 and #3 and is getting close to satisfying #1. It's also arguably simpler, at least for non-document files. Not surprisingly, I find myself using it more and more, internally and externally.
XML is preferrable over CSV when the data is unstructured (unknown schema) and will be read by a human.
Arguably, unless the data contains predominantly text, CSV is also meant for human consumption.
Also relevant, is if your data is 2 or 3 dimensional. CSV is most suitable for 2 dimensional text, and due to its' verbosity, XML works well with 3 dimensional data.
The whole "standardness" of XML is hyperbole, and should not be taken literally. XML does have huge technical issues and many of the solutions aren't particularly elegant, or in many cases useful:
- It uses text to specify its own text-encoding (chicken and egg?)
- None of the more common schema languages for XML work particularly well.
- The ancient and commonplace way of creating mark-up languages using
<tags>
is not particularly helpful as a standard. - XML tries to retroactively shoehorn more powerful mark-up languages such as the SGML based ones, into itself, creating a mess of incompatible legacy.
- It still remains to be determined whether or not XML text escape sequences can work for anything but the most simple cases (ie. friendly data).
To be clear, XML is probably the incorrect choice for 90% of the data interchange it is currently being used for, since those uses break some or all of the above assumptions.
Some strengths:
To name a few from the top of my head.