idish
idish

Reputation: 3260

Serialization .txt vs .xml

I've been reading about serialization, and I don't really understand why we use XML files instead of txt files for example? I've been trying reading data from a txt file and it works perfectly fine. What are the reasons for using XML? Thank you.

Upvotes: 0

Views: 420

Answers (3)

JaredPar
JaredPar

Reputation: 754715

The reason XML is preferred over raw text is that XML is a way of representing structured data. This matches up well with the serialization of object graphs. Text files on the other hand are inherently unstructured and need make up a formatting standard for serialization

Upvotes: 1

Rob Rodi
Rob Rodi

Reputation: 3494

For one, there's tons of tooling around xml (or json, or any well defined serialization format). For another, all the questions have already been answered:

  • How do i nest items?
  • How do i escape characters?
  • How do i automatically serialize?

Plus, there's little ramp up time for other developers to consume your data, since everyone understands these agreed upon formats.

That said, custom serialization can obviously be smaller (binary representations, shorter syntax, etc.) or tailored more specifically to your needs, but then you need to write your own everything, and that's VERY time consuming.

Upvotes: 1

Anders Abel
Anders Abel

Reputation: 69260

An xml file is basically a text file, following the rules for xml formatting.

The main advantage of xml files is that they are hierarchical. Consider e.g. the following XML:

<carOwners>
  <person name="Bill">
    <car brand="Audi"/>
  </person>
  <person name="Charlie">
    <car brand="volvo"/>
    <car brand="saab"/>
  </person>
</carOwners>

Here we have two different data types: people and cars. With the hierarchical structure of XML it is easy to represent the owner of each car.

It is of course possible to do something similar without using XML, but then you would have to do a lot of string manipulation yourself to parse the information. With XML there is at least one xml library available for every major programming language.

Upvotes: 3

Related Questions