views:

193

answers:

9

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

+6  A: 

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..

Jack
Hey, storing images works just fine: `<image> <pixel> <color> <red>255</red> <green>255</green> <blue>0</blue> </color> <location> <x>0</x> <y>0</y> </location> </pixel> ...</image>` :-)
Joey
@Johannes: Seems like you stored your image under the related topics.
Esko
Yes, you can hipotetically store whatever you want but does it make sense? Why did we invent compression algorithms? Everything could be just plain :)
Jack
@Jack, yes, compress XML and you're done!
Pavel Shved
You can compress your binary file too and be likely still smaller ;-)
Joey
Yes, you can do whatever you want. Why should you use a binary sort tree instead of a single linked list? You can always use a single linked list. It's the same story :) You can do whatever you want in many ways but this is just stupid :/
Jack
A: 

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).

Joey
A: 
  • Smaller --> better to transmit.
  • Depending on the access method, faster to parse.
  • Less readable.

And another bunch of reasons. Feel free to add.

Obalix
A: 

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.

Vincent Ramdhanie
A: 

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
Pascal MARTIN
A: 

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.
paxdiablo
Transformation is a moot point for most complex file formats, I think. Just because the underlying syntax has readily available parsers in the XML case doesn't mean that that's where most work of writing a parser goes into. A transformation of OOXML to ODF isn't easy, despite both being XML. The amount of work would likely be similar for a binary format.
Joey
+1  A: 

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)
oefe
A: 

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?

Esko
A: 

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.

paquetp