Convert Text Files to XML (Any Flat File to XML)

Sometimes it's easy to convert flat files to XML, and other times it's not. For the simple cases, the comma-separated value adapter or one of its relatives is a good choice. But there are several cases where that's just not good enough. For example:

  1. Simple CSV files
  2. When fields are fixed-width
  3. When there is more than one record layout in the file

So let's conquer those together.

Fixed Width Record Layout

Suppose your file looks like this bikes_fixed.txt file:

1: Make········Model····Year····Mileage
2: BMW·········R1150RS··2004····14274··
3: Kawasaki····GPz1100··1996····60234··
4: Ducati······ST2······1997····24000··
5: MotoGuzzi···LeMans···2001····12393··
6: BMW·········R1150R···2002····17439··
7: Ducati······Monster··2000····15682··
8: Aprilia·····Futura···2001····17320··

For display, we've used a "·" (raised dot) to represent a space, but notice that although we've broken the text to make the record layout easier to see, there are no line ending markers — no LF's and no CR's.

To turn this into XML, we're going to build a custom adapter. Just follow the steps, and look at the pictures — just like Mapquest or Google Maps.


1. Choose File|New|Convert to XML.

File|New|Convert to XML


2. Choose the input file. What we're going to do is take a file that is representative of the format we want to convert, and use it as a template. Once we have the adapter built against it, that same definition should work against any file of similar format. Note that sometimes there is sufficient context for Stylus Studio® to determine the basic structure and encoding up front, so it offers to "guess" for you based on what it knows about certain common file formats.

New Converter dialog opening fixed-width file


3. When the file opens, Stylus Studio® will assume a default record width for this fixed-width file, since no record separators are obvious. So we will grab the right-hand margin and drag it to the left until the columns line up. Alternatively, we could open the Properties window and set the width manually (but this is more fun).

Fixed width file before setting record size (Click to enlarge)
(click to enlarge)


Fixed width file after setting record size


4. To set the field boundaries, right-click on what looks to be the first character of each field, and choose Begin Field in This Column. Do this once for each field boundary. If you make a mistake, don't worry. That same option will remove the field marker. Or, you can just drag the lines around.

Setting field widths

Field widths all set


5. The fields need names, and in this case they are right inside the file. So we can tell Stylus Studio® to get them from there by right-clicking anywhere in that first row and choosing Get Field Names from This Row. We could also double-click on the field headings and set them manually, or use the Properties window or even the schema tree (which is beyond the scope of this tutorial).

Set field names from file

Completed adapter

At this point, we're ready to run the adapter.


6. Now let's save and run the adapter against our test file. Choose ConvertToXML|Preview Result, or click on the Preview Result button. If you haven't already saved it, you will be prompted to save it now.

Saving adapter definition for later re-use

And here is the final result:

Executing the conversion (Click to enlarge)
(click to enlarge)


This adapter definition can now be used to read this format of flat file anywhere a normal XML file could be read. It can be run through XSLT or XQuery, it can be validated against a DTD or XML Schema, and it can be used in XML pipelines. It can also be used in your own programs by using the embeddable Data Conversion APIs.

Using the adapter in XSLT (Click to enlarge)
(click to enlarge)

Now, go try this out for yourself by downloading an evaluation copy today.

Next XML Data Conversion Utility: Generate XML Schema

PURCHASE STYLUS STUDIO ONLINE TODAY!!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Try Stylus XML Schema IDE

Download a free trial of our award-winning XML Schema IDE today!

Learn XQuery in 10 Minutes!

Say goodbye to 10-minute abs, and say Hello to "Learn XQuery in Ten Minutes!", the world's fastest and easiest XQuery primer, now available for free!

Ask Someone You Know

Does your company use Stylus Studio? Do your competitors? Engineers from over 100,000 leading companies use Stylus Studio, and now you can ask someone from your own organization about their experiences using Stylus Studio.

Support XQuery in the Microsoft .NET Framework

10 Simple things you can do to help support XQuery in the Microsoft .NET 2.0 Framework - Read on!

 
Free Stylus Studio XML Training: