How To Convert a File to XML Format

You have the perfect file with all of your product or statistic data and now you need this data in XML format. Converting a file to XML now is actually much easier than it used to be when XML first became popular. Now there are many tools available to convert files from one format to another. Of course these tools are absolutely useless unless your data is prepared to be converted. Data should be structured and organized so that it can be converted into XML properly.

Step 1

What format is your original file?  Your file type will be the determining factor on how you approach the conversion. If your file is one of the following types you are in luck. Your application will convert it for you:

Microsoft Excel: Do as save as XML file.
Microsoft Access: File | Export | Save as XML File

Note: It is important to note that Microsoft files are exported in a certain format. These documents may or may not be what you expect. They contain elements that pertain to the formatting of your Excel document. This would not be ideal if you are using the data in an application that is particular about the format of the incoming data.

If on the other hand your document is not in any of those formats you will have to either search for a converter tool or convert it by hand (unless you have too much to convert by hand).

Software such as XML Spy can convert your SQL based code into XML documents as well. When you have a large data set a convert is highly recommended. According to its references you can work with:

  • ADO
  • Oracle
  • mySQL
  • SQL Server
  • ODBC
  • Sybase
  • MS Access
  • Excel
  • Tab Delimited, CSV

There are other converter tools such as Rustemsoft's XML Converter or XML Buddy (Eclipse plugin) from Bocaloca software. These applications can export your data and also provide you with a XML Schema or DTD (document type definition) file if desired. These applications are very handy, and make the process of converting your file or database to XML much more user friendly.

Step 2

If you are at this step you must convert it by hand.  In the real world you may not actually even have it originally in Microsoft Excel. Let's take a basic text document and convert it to XML. But first things first how about a quick review of the basic rules of XML:

XML - The extensible markup language is a text-based cross-platform language that is enables you to store data (like addresses in an address book) in a structured manner. The XML document is expected to have correct syntax. Therefore, when making XML documents they should be well-formed. A well-formed document has the following:

  • Closed tags (<painters></painters> or <oil />) 
  • An element's attribute must be enclosed in double quotes (<country countryid="7">)
  • XML is case sensitive. In other words the beginning and ending tag should use the same case

Here is a basic list of 17th century Dutch painters.

Rembrandt Van Rijn, 1606-1669,  Night Watch
Hendrick Avercamp, 1585-1634, winter
Jan de Bisschop, 1628-1671, The Oostpoort

As you can see the data is separated by commas, as known as a comma delimited file. Each line is a separate chunk of data. Each chunk has the painter's name, birth to death year, and finally one of their paintings.  Therefore, we need to build a XML file with the following structure:

  • Painter's Name
  • Painter's Year of Birth
  • Painter's Year of Death
  • Painter's Painting

Step 3

With this format in mind we can create a XML file.  Our root element will be called painters since we have a list of painters.

<?xml version="1.0" encoding="UTF-8"?>
        <name>Rembrandt Van Rijn</name>
        <painting>Night Watch</painting>
        <name>Hendrick Avercamp</name>
        <name>Jan de Bisschop</name>
        <painting>The Oostpoort</painting>

Each painter becomes a sub-element of painters called painter. Each of our painters have a two dates associated with them as well as a painting. Therefore, we are going to make the name, dates, and painting a child element of the element painter. They are going to be called name, yearofBirth, painting, and yearofDeath. With this file we have our converted data ready to be used as we see fit.


Share this article!

Follow us!

Find more helpful articles: