Getting Started with EDIFACTTranslator


Requirements:
EDISDK

Introduction

The EDISDK includes components for reading, writing, translating and validating EDIFACT documents. This guide introduces the EDIFACTTranslator component and demonstrates how to convert EDIFACT documents to XML and back.

For additional options and advanced functionality, refer to the product help documentation.

Tip: Download the latest version of EDISDK to follow along.

Converting EDIFACT to XML

The EDIFACTTranslator component is designed to seamlessly convert EDIFACT messages into XML, as well as transform XML back into EDIFACT format.

The following sections outline the key steps involved in a basic conversion workflow.

Step 1:Loading EDISDK Schemas

EDISDK introduces a new schema XML format, with one file per standard version. These schemas are human-readable and easy to maintain.

The product demos include a sample schema, D97A schema, so an application can start parsing immediately. For guidance on creating custom schemas, see the EDIFACT Schema Format help docs

Note: EDIFACTTranslator can translate from XML to EDI even if no schema is provided. However, translating from EDI to XML without a schema will produce an XML document that lacks descriptive schema elements and instead uses the component’s internal naming conventions.

Schemas can be loaded in two ways:

Option A: Load Schemas beforehand

Call LoadSchema before parsing. For a single message type, pass the schema string or file location along with the message name. To load multiple messages, set the messageName parameter to '*' to load all messages from a file or provide a comma-separated list to load specific messages.

Option B: ResolveSchema Event

For multiple standards or message types, the ResolveSchema event allows dynamic schema loading. This event triggers when an EDIFACT message lacks a corresponding schema. Example:

reader.OnResolveSchema += (s, e) => { string schemapath = basepath+e.MessageVersion+".xml"; translator.LoadSchema(schemapath, e.MessageType); };

Step 2: Handling Validation Errors

During translation the component may fire a ValidateWarning event, which is triggered when validation issues occur. Examples include:

  • Element values that don’t match an expected code list
  • Segments in an unexpected order,
  • Data inconsistencies

Because validation errors are not stored internally, they should be handled or logged as they occur:

translator.OnValidateWarning += (s, e) => { String err = String.Format("[{0}-{1},{2}] {3}: {4} - {5}", e.Position, e.Line, e.Column, e.ErrorCode, e.ErrorMessage, e.ErrorType); m_Warn += err + "\n"; };

This approach allows for applications to capture, review, or log warnings as they occur during parsing.

Step 3: Data Handling

EDIFACTTranslator can read input and write output from several sources:

Source Input Output
Stream SetInputStream(stream) SetOutputStream(stream)
File InputFile = "path/to/file.edi" OutputFile = "path/to/file.edi"
String InputData = ediString OutputData = ediString

Once the data has been loaded, calling the Translate method will begin the process of translating the provided document.

Changing Behaviors

The EDIFACTTranslator component is intended to be flexible and as such the configuration largely depends on intended use. The default settings will translate the input EDIFACT document to an XML string in memory. Translating from XML to EDI any other changes in default behavior may require adjustments to certain key settings such as:

  • Input and Output formats (EDI vs XML).
  • File write mode options (overwrite vs. append vs. create)
  • Encoding options (e.g., iso-8859-1, UTF-8, ect.)

The code below shows how the component may be used for either EDI to XML or XML to EDI:


translator.FileWriteMode = EDIFACTTranslatorFileWriteModes.fwmOverwrite;

//Set up component for EDI to XML translation
translator.InputFormat = EDIFACTTranslatorInputFormats.edifactIFEDI;
translator.OutputFormat = EDIFACTTranslatorOutputFormats.edifactOFXML;
translator.InputFile = "path/to/INVOIC.txt";
translator.OutputFile = "path/to/INVOIC.xml";
translator.Translate();

//Set up component to translate from XML back to EDI
translator.InputFormat = EDIFACTTranslatorInputFormats.edifactIFXML;
translator.OutputFormat = EDIFACTTranslatorOutputFormats.edifactOFEDI;
translator.InputFile = "path/to/INVOIC.xml";
translator.OutputFile = "path/to/INVOIC.txt";
translator.Translate

Conclusion

The EDIFACTTranslator component provides a straightforward and flexible way to convert between EDIFACT and XML formats. By configuring schemas, handling key events, and selecting appropriate input and output formats, documents can be validated, transformed, and integrated into existing workflows efficiently.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.