Home > XML IDE - XML Editor > XML Editor Key Features > Import Export XML > Text File to XML
Import Export XML
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:
So let's conquer those together. Fixed Width Record LayoutSuppose your file looks like this
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. 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. 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). (click to enlarge) 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. 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). 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 button. If you haven't already saved it, you will be prompted to save it now. And here is the final result: (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. (click to enlarge) Now, go try this out for yourself by downloading an evaluation copy today. Next XML Data Conversion Utility: Generate XML Schema Prev: "CSV to XML" Next: "Create XML Schema from XML Content" |
PURCHASE STYLUS STUDIO ONLINE TODAY!!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Try Stylus XML Schema IDEDownload a free trial of our award-winning XML Schema IDE today! Learn Stylus Studio in 6 MinutesCan six minutes change your life? A new action-packed online video demonstration covers how to use Stylus Studio in just six minutes! Watch this video and change the way you develop XML applications forever! Ask Someone You KnowDoes 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 Framework10 Simple things you can do to help support XQuery in the Microsoft .NET 2.0 Framework - Read on! |