Skip to content

Writing EDI Data (Simple)

Michael Edgar edited this page Jul 27, 2020 · 4 revisions

This article assumes the reader has basic knowledge of EDI structures and terminology

Writing EDI

Writing EDI using StAEDI requires a program to emit events to the output in the order in which they must occur in the resulting EDI document. The formatting and optional validation of the data is handled by the StAEDI.

X12 Example

package com.example;

import io.xlate.edi.stream.EDIOutputFactory;
import io.xlate.edi.stream.EDIStreamConstants;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamWriter;

public class WriteInterchangeAcknowledgement {

    public void writeAcknowledgement() throws EDIStreamException {
        // (1) Create an EDIOutputFactory
        EDIOutputFactory factory = EDIOutputFactory.newFactory();

        // (2) Optionally specify delimiters - here the given values are the same as default
        factory.setProperty(EDIStreamConstants.Delimiters.SEGMENT, '~');
        factory.setProperty(EDIStreamConstants.Delimiters.DATA_ELEMENT, '*');

        // Write each segment on a new line
        factory.setProperty(EDIOutputFactory.PRETTY_PRINT, true);

        // (3) Create an EDIStreamWriter. Any OutputStream may be used - here we are writing to the console
        EDIStreamWriter writer = factory.createEDIStreamWriter(System.out);

        // (4) Start the interchange
        writer.startInterchange();

        // (5) Write the three segments necessary for the interchange acknowledgement (ISA, TA1, IEA)
        writer.writeStartSegment("ISA")
              .writeElement("00")
              .writeElement("          ")
              .writeElement("00")
              .writeElement("          ")
              .writeElement("ZZ")
              .writeElement("ReceiverID     ")
              .writeElement("ZZ")
              .writeElement("Sender         ")
              .writeElement("203001")
              .writeElement("1430")
              .writeElement("^")
              .writeElement("00501")
              .writeElement("000000001")
              .writeElement("0")
              .writeElement("P")
              .writeElement(":")
              .writeEndSegment();

        writer.writeStartSegment("TA1")
              .writeElement("000000050")
              .writeElement("200229")
              .writeElement("1200")
              .writeElement("A")
              .writeElement("000")
              .writeEndSegment();

        writer.writeStartSegment("IEA")
              .writeElement("1")
              .writeElement("000000001")
              .writeEndSegment();

        // (6) End the interchange
        writer.endInterchange();

        // (7) Close the EDIStreamWriter. This must be done to ensure the output is written
        writer.close();
    }
}

EDIFACT Example

// (1) Create an EDIOutputFactory
EDIOutputFactory factory = EDIOutputFactory.newFactory();
int messageCount = 0; // Counter for the UNH/UNT messages contained in the interchange
int messageSegmentCount = 0; // Counter for the segments in a UNH/UNT message

// (2) Optionally specify delimiters - here the given values are the same as default
factory.setProperty(EDIStreamConstants.Delimiters.SEGMENT, '\'');
factory.setProperty(EDIStreamConstants.Delimiters.DATA_ELEMENT, '+');
factory.setProperty(EDIStreamConstants.Delimiters.COMPONENT_ELEMENT, ':');
factory.setProperty(EDIStreamConstants.Delimiters.RELEASE, '?');
factory.setProperty(EDIStreamConstants.Delimiters.REPETITION, ' ');

// Write each segment on a new line (optional)
// factory.setProperty(EDIOutputFactory.PRETTY_PRINT, true);

// (3) Create an EDIStreamWriter. Any OutputStream may be used - here we are writing to a file
OutputStream stream = new FileOutputStream("/tmp/edifact.out");
EDIStreamWriter writer = factory.createEDIStreamWriter(stream);

// (4) Start the interchange
writer.startInterchange();

// Optionally write a UNA segment. When delimiters are specified via the `EDIOutputFactory`'s
// properties, a UNA segment will be automatically written.
writer.writeStartSegment("UNA").writeEndSegment();

// (5) Write the beginning segment for the interchange.
writer.writeStartSegment("UNB");

// Writing composite elements is done by first starting the element,
// then writing the components. Finally, the element is ended.
writer.writeStartElement()
      .writeComponent("UNOC")
      .writeComponent("3")
      .endElement();

writer.writeStartElement()
      .writeComponent("0123456789012")
      .writeComponent("14")
      .endElement();

writer.writeStartElement()
      .writeComponent("0123456789012")
      .writeComponent("14")
      .endElement();

writer.writeStartElement()
      .writeComponent("200702")
      .writeComponent("0734")
      .endElement();

// A simple data element may be written with a single method call.
writer.writeElement("00000563");

// Complete the UNB segment
writer.writeEndSegment();

// (6) Write the message header segment
writer.writeStartSegment("UNH");
// Keep track of the message count for the UNZ trailer segment
messageCount++;
// Keep track of the segment count for this message
messageSegmentCount++;

writer.writeElement("0001");

writer.writeStartElement()
      .writeComponent("INVOIC")
      .writeComponent("D")
      .writeComponent("96A")
      .writeComponent("UN")
      .writeComponent("EAN008")
      .endElement();

writer.writeEndSegment();

// Begin Message BGM
writer.writeStartSegment("BGM");
messageSegmentCount++;
writer.writeElement("380");
writer.writeElement("1676245");
writer.writeElement("9");
writer.writeEndSegment();

// From Here do further INVOIC related segments like DTM and so on

// (7) Write the message trailer segment
writer.writeStartSegment("UNT")
      .writeElement(String.valueOf(++messageSegmentCount))
      .writeElement("0001")
      .writeEndSegment();

// (8) Write the message trailer segment
writer.writeStartSegment("UNZ")
      .writeElement(String.valueOf(messageCount))
      .writeElement("00000563")
      .writeEndSegment();

// (9) End the interchange
writer.endInterchange();

// (10) Close the EDIStreamWriter. This must be done to ensure the output is flushed and written
writer.close();

Alternate Character Encoding

By default, StAEDI will write EDI output using the UTF-8 standard character set. The character set may be overridden by creating an instance of EDIStreamWriter (via EDIOutputFactory) with the name of an alternate Charset. Names of Charsets that return true for a call to Charset#isSupported may be used.