Question

Why all use binary files, if all can use XML ?

Was it helpful?

Solution

Because of performance, of course, then XML is good when you have to define a tree structure but not all kind of data fits well with it. Would you store a 3d model inside a XML file? Or an image?

XML is good to handle text data, what about effective binary data like images, sounds, compressed files, whatever..

It's really verbose and heavy to be parsed and you don't want to use it when performance matters (think about the netcode of a game for example).

I would shoot myself if I have to read an XML file containing for example structures for vectors or points.

Using a parser instead that dumping content into memory with something like:

fread(&myBuf, sizeof(vector_struct), 10, in);

would make me feel stupid..

OTHER TIPS

Have you seen XML? It appears to be an insidious scheme by hardware manufacturers to sell larger hard disks :-)

However, humour aside, I would choose to use binary files if:

  • I wasn't too concerned about making the information available to outside systems, or portable to other platforms.
  • I wanted to read and write it in at maximum speed (without having to parse/produce XML).
  • I didn't need it human-readable, or easily transformed.
  • I was working on a system where XML didn't make sense (embedded C) or where XML processing libraries weren't readily available.

In many cases, XML will be a good choice, but there are scenarios where you need a binary format or should at least consider it:

  • If you need random access (and can't load the file in memory - e.g databases)
  • If file size is a concern (e.g. images, movies)
  • If the data is binary in nature (e.g. images, sound)
  • If performance is a concern (all of the above)

The following aren't good reasons to use binary:

  • XML is hard to parse (there are excellent XML libraries for almost any language available)
  • Binary prevents the user from tampering (it doesn't)

Read this: Joel on software: Back to basics. Yes, it's a lot of text. Yes, it doesn't seem to relate to your question but no, this isn't a bad answer to your question - if I were Joel, I'd just quote that entire post here and claim a bazillion rep for it.

Here's a very shining example: Binary formats are always aligned,every x..x+y bits represent one group of data. Moving to other group is just as easy as getting the original startpoint and the index of the group and multiply the two values with that ( x*n..(x+y)*n ) to get all the data related to that group. How do you, exactly, do this with XML?

Get the best of both worlds. Use XSD binding with a tool that serializes/deserializes to both binary and XML - like www.codesynthesis.com XSD.

XML is a hell to parse. As a format that's easy for people to write and for programs to read it's actually one of the worse ones.

Binary files also have the advantage that you can skip much of parsing and just treat some parts of the file as direct memory dumps (if you're so inclined—you should do so carefully, though).

Not every type of data can be represented in xml. And it saves a lot of space to store values in binary rather than XML. If the data is going to be parsed by an XML parser then it should be stored in XML, else why waste the space.

Because parsing XML :

  • Takes time
  • Requires a lot of calculations

So, using binary files is better for performances.


And, also :

  • XML is a pretty verbose format : think to all those tags -- which make the file so much bigger.
  • representing your data as text is not always easy/possible
  • Smaller --> better to transmit.
  • Depending on the access method, faster to parse.
  • Less readable.

And another bunch of reasons. Feel free to add.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top