XML or own-format file?

Tags: file xml
By : FerranB
Source: Stackoverflow.com

When is a good idea to save information in a XML file and when in a own-format file?

For XML (or other standard) I see:

  • (+) Standard format.
  • (-) It's tedious to hand modify.

For own-format files I see:

  • (-) We need to build a own-parser (non-standard).
  • (+) It can be easy to hand modify the files.
By : FerranB


It really depends on your data.

See ESR's The Art of Unix Programming: Ch. 5 Textuality - Data File Metaformats. This quote about sums it up:

XML can be a simplifying choice or a complicating one. There is a lot of hype surrounding it, but don't become a fashion victim by either adopting or rejecting it uncritically. Choose carefully and bear the KISS principle in mind.

XML certainly has its uses, and it is wonderful for expressing complicated hierarchical datasets, but it's overkill if all you need to do is store half a dozen key:value pairs, and inappropriate for row-based tabular data.

XML is usually my first choice. Part of it is because it is the standard configuration file format for my platform choice (.NET). I have found that, almost exclusively, a well-defined XML file is better than a customized format. I will shy away from CSV and flat files, as well, unless they are a requirement of the project.

My reasons for XML as my choice (note that some are platform specific):

  • Standard implementation for my platform. Plenty of tooling available to work with XML, XSD, XSLT.

  • Schema enforcement (XSD). Allows me to enforce the file structure. Very helpful when the format is consumed by others, as well.

  • Navigation (XPath, Linq to Xml). Easy to extract and write nodes and their attributes. Less risk in writing this type of code over customer readers and writers.

  • Transformable (XSLT). Can convert the file to other presentational views with little effort.

  • Interoperable. The structure of XML is a natural fit for describing objects. Objects serialized into XML is easily portable and can survive across application boundaries.

  • Easily Editable. A well-defined XML is easy to read and easy to edit. A simple text editor is enough to get started, and there are many XML editing tools available with a variety of features and price points.

I don't understand the perception that XML would be any less easy to modify by hand than a custom format. XML might be more verbose than a format that you come up with, but it provides contextual relevance to the data it contains. If you can look at (well-formed) XHTML, it is not much different when you look at XML.

As annakata stated, you can use XSLT and XPATH if you choose the XML route. I've found that with some clever use of XSLT you can create "self-documenting" configuration files.

By creating an .xsl file and adding a declaration such as this to the XML file, a user can simply double-click the XML file and view the results of the transformation in their browser (I know IE and Firefox both support this)

<?xml-stylesheet type="text/xsl" href="config-documentation.xsl"?>

Just thought that might be helpful.

By : Rich

This video can help you solving your question :)
By: admin