Reading EDI data in Java

Today, most Java developers expect to use JSON to exchange data with other systems and enterprises. But what happens when you can't use JSON? Easy to forget, there are other formats of data exchange, some of which are more difficult to handle. One such format is called EDI. EDI itself has many styles (such as X12 and EDIFACT), so reading its code may not always be "one size fits all.".

During the special period, I learned about using java to read EDI data through the online Java sharing of yuruike.
Benefit a lot, specially collate out for you to learn.

On the surface, reading EDI data seems simple. Developers may see the sample file and try to read it using the basic string parsing API provided in their programming language's standard library. Unfortunately, this may not always work, and it is difficult to validate the data and process the structure of the document effectively.

What is EDI? An introduction

EDI is a generic term that covers several standard data formats used to exchange data between enterprises (or any two parties). X12 and EDIFACT are the two most commonly used standards. Both standards represent data in a series of named segments (basically records that contain individual fields). For example, a simple subdivision might look like this:
SEGSMITHJOHN20190101~
In this example, the name of the segment is "SEG," which contains three fields - two strings and a date. Each field (known as an element) is separated from each other by a delimiter (asterisk), and the end of the segment is indicated by a delimiter (~ (superscript).

EDI also has a structure similar to XML or JSON, where segments are nested within a start / end boundary called a loop. In this example, X12 confirms the exchange as follows; indentation is added to emphasize the structure. However, in practice, by looking at unformatted EDI files, the structure is not obvious.

ISA00 00 ZZReceiver ZZSender 1910311301*^005010000000010P*:~
GSFAReceiverDeptSenderDept20191031130123000001X005010X230~
ST9970001~
AK1HC000001~
AK28370001~
AK3NM18**8~
AK48667MI~
AK5R5~
AK9R110~
SE80001~
GE1000001~
IEA1000000001~

Read EDI as an event flow

One way to read EDI is to process the dat anonymous anonymous SA event stream. The Java library of this StAEDI (pronounced "stable") takes the same EDI approach, that is, the standard Java API's StAX needs to handle XML event flows. A simple program that lists only segment names might look like this:

EDIInputFactory factory = EDIInputFactory.newFactory();
InputStream stream = new FileInputStream("my_edi_file.txt");
EDIStreamReader reader = factory.createEDIStreamReader(stream);
EDIStreamEvent event;
while (reader.hasNext()) {
  event = reader.next();
  if (event == EDIStreamEvent.START_SEGMENT) {
    System.out.println("Segment: " + reader.getText());
  }
}

If you are familiar with the StAX API for XML, this should look familiar. Used in an editreamreader loop similar to an iterator or database result set. Each call to the next method returns the next data event from the EDI file. In addition to the events at the beginning of the segment, there are also some events that need to be handled:

Start and end of exchange (ISA / IEA in x 12, UNB / Unz for EDIFACT)
Start and end of message group (GS / Ge at X12, ung / une to EDIFACT)
Start and end of transaction (ST / Se in x 12, unh / unt for EDIFACT)
 start and end of cycle (depending on configuration)
 start and end of segment
Start and end of composite elements
 individual data elements
 segmentation error
Data element error

So what about structure and validation?

Dealing with EDI with StAEDI will only give you the basic structure and validation (exchange, group, and transactional envelope structures). If you need to handle the structure and validation of the data in the transaction (which you may need to do), you must provide a schema. The schema is based roughly on the standard XML schema syntax and provides details about the middle of a transaction and the order of elements. In addition, you can impose constraints on the length and data type of elements.

997 can use the following XML to define the EDI schema for the above example X12 transaction. The structure of the transaction defined by the transaction element and its child elements. Each segment references the elements defined below the segmentType, and if necessary, each segmentType references the elementtypes and compositetypes. identifier for brevity, enumeration values for most element types have been omitted.

<schema xmlns="http://xlate.io/EDISchema/v2">
  <transaction>
    <sequence>
      <segment ref="AK1" minOccurs="1" />
      <loop code="2000" maxOccurs="999999">
        <sequence>
          <segment ref="AK2" />
          <loop code="2100" maxOccurs="999999">
            <sequence>
              <segment ref="AK3" />
              <segment ref="AK4" maxOccurs="99" />
            </sequence>
          </loop>
          <segment ref="AK5" minOccurs="1" />
        </sequence>
      </loop>
      <segment ref="AK9" minOccurs="1" />
    </sequence>
  </transaction>
  <elementType name="E0002" number="2" base="numeric" maxLength="6" />
  <elementType name="E0028" number="28" base="numeric" maxLength="9" />
  <elementType name="E0097" number="97" base="numeric" maxLength="6" />
  <elementType name="E0123" number="123" base="numeric" maxLength="6" />
  <elementType name="E0143" number="143" base="identifier" minLength="3" maxLength="3" />
  <elementType name="E0329" number="329" base="string" minLength="4" maxLength="9" />
  <elementType name="E0447" number="447" base="string" maxLength="4" />
  <elementType name="E0479" number="479" base="identifier" minLength="2" maxLength="2" />
  <elementType name="E0480" number="480" base="string" maxLength="12" />
  <elementType name="E0715" number="715" base="identifier">
    <enumeration>
      <value>A</value>
      <value>E</value>
      <value>M</value>
      <value>P</value>
      <value>R</value>
      <value>W</value>
      <value>X</value>
    </enumeration>
  </elementType>
  <elementType name="E0716" number="716" base="identifier" maxLength="3" />
  <elementType name="E0717" number="717" base="identifier">
    <enumeration>
      <value>A</value>
      <value>E</value>
      <value>M</value>
      <value>R</value>
      <value>W</value>
      <value>X</value>
    </enumeration>
  </elementType>
  <elementType name="E0718" number="718" base="identifier" maxLength="3" />
  <elementType name="E0719" number="719" base="numeric" maxLength="10" />
  <elementType name="E0720" number="720" base="identifier" maxLength="3" />
  <elementType name="E0721" number="721" base="string" minLength="2" maxLength="3" />
  <elementType name="E0722" number="722" base="numeric" maxLength="2" />
  <elementType name="E0723" number="723" base="identifier" maxLength="3" />
  <elementType name="E0724" number="724" base="string" maxLength="99" />
  <elementType name="E0725" number="725" base="numeric" maxLength="4" />
  <elementType name="E1528" number="1528" base="numeric" maxLength="2" />
  <elementType name="E1686" number="1686" base="numeric" maxLength="4" />
  <elementType name="E1705" number="1705" base="string" maxLength="35" />
  <compositeType name="C030">
    <sequence>
      <element ref="E0722" minOccurs="1" />
      <element ref="E1528" />
      <element ref="E1686" />
    </sequence>
  </compositeType>
  <segmentType name="AK1">
    <sequence>
      <element ref="E0479" minOccurs="1" />
      <element ref="E0028" minOccurs="1" />
      <element ref="E0480" />
    </sequence>
  </segmentType>
  <segmentType name="AK2">
    <sequence>
      <element ref="E0143" minOccurs="1" />
      <element ref="E0329" minOccurs="1" />
      <element ref="E1705" />
    </sequence>
  </segmentType>
  <segmentType name="AK3">
    <sequence>
      <element ref="E0721" minOccurs="1" />
      <element ref="E0719" minOccurs="1" />
      <element ref="E0447" />
      <element ref="E0720" />
    </sequence>
  </segmentType>
  <segmentType name="AK4">
    <sequence>
      <composite ref="C030" minOccurs="1" />
      <element ref="E0725" />
      <element ref="E0723" minOccurs="1" />
      <element ref="E0724" />
    </sequence>
  </segmentType>
  <segmentType name="AK5">
    <sequence>
      <element ref="E0717" minOccurs="1" />
      <element ref="E0718" />
      <element ref="E0718" />
      <element ref="E0718" />
      <element ref="E0718" />
      <element ref="E0718" />
    </sequence>
  </segmentType>
  <segmentType name="AK9">
    <sequence>
      <element ref="E0715" minOccurs="1" />
      <element ref="E0097" minOccurs="1" />
      <element ref="E0123" minOccurs="1" />
      <element ref="E0002" minOccurs="1" />
      <element ref="E0716" />
      <element ref="E0716" />
      <element ref="E0716" />
      <element ref="E0716" />
      <element ref="E0716" />
    </sequence>
  </segmentType>
</schema>`Insert a code slice here`

It's easy to use a Schema when parsing EDI files. When the program traverses the event flow, the application code must provide the Schema object at the beginning of the transaction. (before the end of the section)

<schema xmlns="http://xlate.io/EDISchema/v2">
  <transaction>
    <sequence>
      <segment ref="AK1" minOccurs="1" />
      <loop code="2000" maxOccurs="999999">
        <sequence>
          <segment ref="AK2" />
          <loop code="2100" maxOccurs="999999">
            <sequence>
              <segment ref="AK3" />
              <segment ref="AK4" maxOccurs="99" />
            </sequence>
          </loop>
          <segment ref="AK5" minOccurs="1" />
        </sequence>
      </loop>
      <segment ref="AK9" minOccurs="1" />
    </sequence>
  </transaction>
  <elementType name="E0002" number="2" base="numeric" maxLength="6" />
  <elementType name="E0028" number="28" base="numeric" maxLength="9" />
  <elementType name="E0097" number="97" base="numeric" maxLength="6" />
  <elementType name="E0123" number="123" base="numeric" maxLength="6" />
  <elementType name="E0143" number="143" base="identifier" minLength="3" maxLength="3" />
  <elementType name="E0329" number="329" base="string" minLength="4" maxLength="9" />
  <elementType name="E0447" number="447" base="string" maxLength="4" />
  <elementType name="E0479" number="479" base="identifier" minLength="2" maxLength="2" />
  <elementType name="E0480" number="480" base="string" maxLength="12" />
  <elementType name="E0715" number="715" base="identifier">
    <enumeration>
      <value>A</value>
      <value>E</value>
      <value>M</value>
      <value>P</value>
      <value>R</value>
      <value>W</value>
      <value>X</value>
    </enumeration>
  </elementType>
  <elementType name="E0716" number="716" base="identifier" maxLength="3" />
  <elementType name="E0717" number="717" base="identifier">
    <enumeration>
      <value>A</value>
      <value>E</value>
      <value>M</value>
      <value>R</value>
      <value>W</value>
      <value>X</value>
    </enumeration>
  </elementType>
  <elementType name="E0718" number="718" base="identifier" maxLength="3" />
  <elementType name="E0719" number="719" base="numeric" maxLength="10" />
  <elementType name="E0720" number="720" base="identifier" maxLength="3" />
  <elementType name="E0721" number="721" base="string" minLength="2" maxLength="3" />
  <elementType name="E0722" number="722" base="numeric" maxLength="2" />
  <elementType name="E0723" number="723" base="identifier" maxLength="3" />
  <elementType name="E0724" number="724" base="string" maxLength="99" />
  <elementType name="E0725" number="725" base="numeric" maxLength="4" />
  <elementType name="E1528" number="1528" base="numeric" maxLength="2" />
  <elementType name="E1686" number="1686" base="numeric" maxLength="4" />
  <elementType name="E1705" number="1705" base="string" maxLength="35" />
  <compositeType name="C030">
    <sequence>
      <element ref="E0722" minOccurs="1" />
      <element ref="E1528" />
      <element ref="E1686" />
    </sequence>
  </compositeType>
  <segmentType name="AK1">
    <sequence>
      <element ref="E0479" minOccurs="1" />
      <element ref="E0028" minOccurs="1" />
      <element ref="E0480" />
    </sequence>
  </segmentType>
  <segmentType name="AK2">
    <sequence>
      <element ref="E0143" minOccurs="1" />
      <element ref="E0329" minOccurs="1" />
      <element ref="E1705" />
    </sequence>
  </segmentType>
  <segmentType name="AK3">
    <sequence>
      <element ref="E0721" minOccurs="1" />
      <element ref="E0719" minOccurs="1" />
      <element ref="E0447" />
      <element ref="E0720" />
    </sequence>
  </segmentType>
  <segmentType name="AK4">
    <sequence>
      <composite ref="C030" minOccurs="1" />
      <element ref="E0725" />
      <element ref="E0723" minOccurs="1" />
      <element ref="E0724" />
    </sequence>
  </segmentType>
  <segmentType name="AK5">
    <sequence>
      <element ref="E0717" minOccurs="1" />
      <element ref="E0718" />
      <element ref="E0718" />
      <element ref="E0718" />
      <element ref="E0718" />
      <element ref="E0718" />
    </sequence>
  </segmentType>
  <segmentType name="AK9">
    <sequence>
      <element ref="E0715" minOccurs="1" />
      <element ref="E0097" minOccurs="1" />
      <element ref="E0123" minOccurs="1" />
      <element ref="E0002" minOccurs="1" />
      <element ref="E0716" />
      <element ref="E0716" />
      <element ref="E0716" />
      <element ref="E0716" />
      <element ref="E0716" />
    </sequence>
  </segmentType>
</schema>

If you like this article, you can like it. Welcome to leave a message and comment. Remember to pay attention to me. Keep updating the technical dry goods, interesting things in the workplace, massive interview materials and so on every day
If you are very interested in java technology, you can also go to qun 907135806 for exchange learning and common learning progress. Latest java free materials
Don't use "no time" to cover up your mental laziness! Take advantage of your youth and work hard to explain yourself to the future
The article wrote here, welcome to improve communication. Finally, I present a complete set of Java architecture mind map sorted out recently to share with you for reference. There are more JVM, Mysql, Tomcat, Spring Boot, Spring Cloud, Zookeeper, Kafka, RabbitMQ, RockerMQ, Redis, ELK, Git and other Java dry goods

137 original articles published, praised 13, visited 10000+
Private letter follow

Tags: Java xml JSON Spring

Posted on Sun, 02 Feb 2020 02:40:06 -0500 by PakiGangsta