WOLFRAM LANGUAGE TUTORIAL

Developing an Export Converter

The Wolfram Language provides functions that allow developers to implement file format converters to be integrated with the the Wolfram Language Import and Export framework. You can implement format converters and use Export to export data from customized formats.

The interface between Export and low-level converter functions is specified by RegisterExport (under the ImportExport` context). In essence, RegisterExport tells the Import and Export framework how to make various function calls when exporting Wolfram Language expressions to a file format.

The Wolfram Language also includes source code for various converters. These are placed in the folders $InstallationDirectory/SystemFiles/Formats/format, where format is one of the following: BDF, DIF, MTP, SMILES, SurferGrid, TGF, or TLE. The registration code can be found in the files Import.m and/or Export.m, and the converter sources reside in the files Converter.m.

Format Registration

An Export format must first be registered. Note that, unlike registering an Import format, RegisterExport accepts only one function.

ImportExport`RegisterExport["format",function]register function to be used by the Export framework when exporting a file to the type format

Writing a Converter Function

For this example, imagine a file format that encodes an ASCII string by each character's ASCII code, with each code separated by a single space. A converter function for this format can be added to the Import and Export framework using RegisterExport.

In[9]:=
Click for copyable input

When you use Export[file.ext,"str","MyExportFormat"], the framework passes both the output file name and the data, str, to the exporter, so an example exporter function can be written as below.

In[22]:=
Click for copyable input

"MyExportFormat" can now be used in Export just like any other file format.

In[23]:=
Click for copyable input

Elements and Options for Converter Functions

Unlike Import, which always imports elements, Export can directly export an expression (as in the example above) or a list of rules in the form of elementName->elementValue.

Elements can be designated as options, meaning that they will always be passed to the converter functions along with all other options. Here is a trivial converter to illustrate the handling of elements and options.

In[11]:=
Click for copyable input
In[12]:=
Click for copyable input
Export directly with expressions.
In[13]:=
Click for copyable input
In[14]:=
Click for copyable input
Export via elements.
In[15]:=
Click for copyable input
In[16]:=
Click for copyable input
Export via elements, with options.
In[17]:=
Click for copyable input
In[18]:=
Click for copyable input
Export via elements, with elements registered as options.
In[19]:=
Click for copyable input
In[20]:=
Click for copyable input

Note that by specifying "Options"->{"opt1","opt2"}, the elements "opt1" and "opt2" are automatically passed as options to the exporters.

Translate this page: