Reading XML vs reading CSV file java
What is quicker and better in performance?
Reading XML with DocumentBuilder or CSV w开发者_如何学JAVAith FileReader/BufferReader in Java?
I agree with both blunders and duffymo. I just wanted to add the following.
As it was already said both are the data format, so think about your data. How large and how complicated is it? If it is hierarchical, forget about CSV. If it is not very large do the same.
Thinking about XML remember that DOM is not the only way to parse it. SAX is faster. And you can use Digester (built on top of SAX) that allows you to define mapping between your data model and XML schema using XML and then runs very quickly.
If you data very large and your parser must be very fast check JSON. It should be faster than XML because it is less verbose.
I don't know about performance, but one factor is ease of finding standard, well-used parsers. There's an XML parser built into the JDK now, but I'm not aware of a CSV parser. I think XML is far more ubiquitous than CSV.
Another factor is the nature of the data: XML suggests hierarchical, while CSV suggests tables. I think the "best" way to read in data depends more on something like this.
While I can't speak to quicker builds and easy maintenance, nor performance; though I'm guessing it really depends on HOW your using the documents being parsed; e.g. reading document nodes would be way faster than csv, loading a document might be faster in CSV. All that said, CSV is evil, meaning it's highly unstable data store. XML has more overhead, but is way, way more stable.
RELATED_QUESTION: When and Why is XML preferable to CSV?
Reading a CSV file with the FileReader
class is faster as the reader only reads the file and the parsing of the values is a quite easy step here.
On the other hand, reading an XML file using a DOMReader
or SAXParser
(you do not read documents using the builder class, it is used to create XML documents, as far as I know) is slower because the processing of XML data is a much more complicated step. XML files tend to be very verbose.
The advantage of the XML file is that you can put more stress to data validation (when using XSD for XML structure definition), i.e. testing the values for correctness when reading the file. Also one can edit the XML file without any further explanations as the XML element names (and possible comments) say more than semi-colons in the CSV file.
I've been wondering the same. I just did a crude test using Excel to read and parse a simple file with 8,000 records. The XML load took ~8 seconds. The CSV load took less than 1 second.
I think that CSV is a perfectly valid choice for simple tabular data, and carries a lot less overhaed. XML is GREAT, for more complex scenarios...
精彩评论