views:

582

answers:

7

Is there a standard or open format which can be used to describe the formating of a flat file. My company integrates many different customer file formats. With an XML file it's easy to get or create an XSD to describe the XML file format. I'm looking for something similar to describe a flat file format (fixed width, delimited etc). Stylus Studio uses a proprietary .conv format to do this. That .conv format can be used at runtime to transform an arbitrary flat file to an XML file. I was just wondering if there was any more open or standards based method for doing the same thing.

I'm looking for one method of describing a variety of flat file formats whether they are fixed width or delimited, so CSV is not an answer to this question.

+2  A: 
Stephan202
A: 

CSV

CSV is a delimited data format that has fields/columns separated by the comma character and records/rows separated by newlines. Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes. However, if a line contains a single entry which is the empty string, it may be enclosed in double quotes. If a field's value contains a double quote character it is escaped by placing another double quote character next to it. The CSV file format does not require a specific character encoding, byte order, or line terminator format.


The CSV entry on wikipedia allowed me to find a comparison of data serialization formats that is pretty much what you asked for.

voyager
+1  A: 

At the end of the day, you will probably have to define your own file standard that caters specifically to your storage needs. What I suggest is using xml, YAML or JSON as your internal container for all of the file types you receive. On top of this, you will have to implement some extra validation logic to maintain meta-data such as the column sizes of the fixed width files (for importing from and exporting to fixed width). Alternatively, you can store or link a set of metadata to each file you convert to the internal format.

There may be a standard out there, but it's too hard to create 'one size fits all' solutions for these problems. There are entity relationship management tools out there (Talend, others) that make creating these mappings easier, but you will still need to spend a lot of time maintaining file format definitions and rules.

As for enforcing column width, xml might be the best solution as you can describe the formats using xml schemas (with the length restriction). For YAML or JSON, you may have to write your own logic for this, although I'm sure someone else has come up with a solution.

See XML vs comma delimited text files for further reference.

Dana the Sane
I don't have a choice as to what format to use. Customers are providing flat files in the form of delimeted, fixed width or XML. I have to go from those formats to an internal format. Simple to do with XML, just use an XSLT transformation. Fairly simple to do with delimeted, just describe the delimiter and then build an XML file which can have an XSLT applied. More difficult to do with fixed width, you have to describe each field length. I'm looking for an open standard which can describe fixed width and delimited flat files so I don't have to create my own persistance for that meta data
Stimy
Alternatively, you could use a tool that knows how to manipulate flat files and convert them to other formats. SSIS comes to mind (SQL Server Integration Services).
John Saunders
Question updated. @John, yes tool assistance is key here.
Dana the Sane
+7  A: 

XFlat: http://www.infoloom.com/gcaconfs/WEB/philadelphia99/lyons.HTM#N29 http://www.unidex.com/overview.htm

For complex cases (e.g. log files) you may consider a lexical parser.

queen3
Hey! This one actually answers the question. I found XFlat on an earlier search on the issue, but can't find a whole lot of information on who owns it, if it's a real standard etc. Unidex also provides tools for taking the XFlat description and a flat file in order to transform it into XML (http://www.unidex.com/xflat.htm)
Stimy
+2  A: 

COBOL (whether you like it or not) has a standard format for describing fixed-width record formats in files.

Other file formats, however, are somewhat simpler to describe. A CSV file, for example, is just a list of strings. Often the first row of a CSV file is the column names -- that's the description.

There are examples of using JSON to formulate metadata for text files. This can be applied to JSON files, CSV files and fixed-format files.

Look at http://www.projectzero.org/sMash/1.1.x/docs/zero.devguide.doc/zero.resource/declaration.html

This is IBM's sMash (Project Zero) using JSON to encode metadata. You can easily apply this to flat files.

S.Lott
A: 

The only similar thing I know of is Hachoir, which can currently parse 70 file formats:

http://bitbucket.org/haypo/hachoir/wiki/Home

I'm not sure if it really counts as a declarative language, since it's plugin parser based, but it seems to work, and is extensible, which may meet your needs just fine.

As an aside, there are interesting standardised, extensible flat-file FORMATS, such as IFF (Interchange File Format).

Lee B
+1  A: 

I don't know if there is any standard or open format to describe a flat file format. But one industry has done this: the banking industry. Financial institutions are indeed communicating using standardized message over a dedicated network called SWIFT. SWIFT messages were originally positional (before SWIFTML, the XMLified version). I don't know if it's a good suggestion as it's kinda obscure but maybe you could look at the SWIFT Formatting Guide, it may gives you some ideas.

Having that said, check out Flatworm, an humble flat file parser. I've used it to parse positional and/or CSV file and liked its XML descriptor format. It may be a better suggestion than SWIFT :)

Pascal Thivent