Skip to content

Reading EDI Data (Simple)

Michael Edgar edited this page May 26, 2020 · 6 revisions

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

Reading EDI

Reading EDI data using StAEDI allows you to receive a stream of events representing the data in an EDI document. Suppose we have a very simple example of an X12 interchange acknowledgement.

ISA*00*          *00*          *ZZ*Receiver       *ZZ*Sender         *200301*1430*^*00501*000000001*0*P*:~
TA1*000000050*200229*1200*A*000~
IEA*1*000000001~

To retrieve the acknowledgement status from the file (elements TA104 and TA105), a basic program using StAEDI could be as follows.

package com.example;

import java.io.FileInputStream;
import java.io.InputStream;

import io.xlate.edi.stream.EDIInputFactory;
import io.xlate.edi.stream.EDIStreamEvent;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamReader;

public class ReadInterchangeAcknowledgementTest {

    public boolean isAcknowledgementSuccess() throws Exception {
        EDIInputFactory factory = EDIInputFactory.newFactory();
        String ta104 = null;
        String ta105 = null;

        /* (1) Open the EDI file - any InputStream can be used. */
        try (InputStream stream = new FileInputStream("x12_interchange_ack.txt")) {
            /* (2) Create a new EDIStreamReader */
            EDIStreamReader reader = factory.createEDIStreamReader(stream);
            EDIStreamEvent event;
            String segment = null;

            /* (3) Loop over the reader's events */
            while (reader.hasNext()) {
                event = reader.next();

                if (event == EDIStreamEvent.START_SEGMENT) {
                    /* (4)
                     * Each time a segment is encountered, save the
                     * segment tag in a local variable */
                    segment = reader.getText();
                } else if (event == EDIStreamEvent.ELEMENT_DATA) {
                    if ("TA1".equals(segment)) {
                        /* (5)
                         * When reading element data, if the current
                         * segment is TA1 and the current element is
                         * in either position 4 or 5, save the element
                         * data in a local variable */
                        if (reader.getLocation().getElementPosition() == 4) {
                            ta104 = reader.getText();
                        } else if (reader.getLocation().getElementPosition() == 5) {
                            ta105 = reader.getText();
                        }
                    }
                }
            }
        }

        return "A".equals(ta104) && "000".equals(ta105);
    }
}

In this example, the code is focused on retrieving only two elements from the EDI data (TA104 and TA105) and ultimately testing whether they contain specific values.

Disable Validation of Control Codes

Out of the box, instances of EDIStreamReader will validate the control structures of X12 and EDIFACT messages (interchange, group, and transaction headers and trailers). This validation includes checking that some fields contain one of an enumerated list of values (e.g. a known transaction set code in X12 segment ST, element 1).

If you wish to disable the validation of the code values but keep the validation of the structure, including field sizes and types, set the EDIInputFactory.EDI_VALIDATE_CONTROL_CODE_VALUES property to false on an instance of EDIInputFactory prior to creating a new EDIStreamReader, as shown below.

// Create an EDIInputFactory
EDIInputFactory factory = EDIInputFactory.newFactory();
factory.setProperty(EDIInputFactory.EDI_VALIDATE_CONTROL_CODE_VALUES, false);

// Obtain an InputStream of the EDI document to read.
InputStream stream = new FileInputStream(...);

// Create an EDIStreamReader from the stream using the factory
EDIStreamReader reader = factory.createEDIStreamReader(stream);

// Continue processing with the reader...

Alternate Character Encoding

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

Clone this wiki locally