EDIdEv - Electronic Data Interchange Development

Converting an EDI file to Text

This is a source code in Visual Basic that converts an EDI file to a text file using the EDIdEv Framework EDI (FREDI) component as the EDI parser.  For this example, the text file will have an XML structure; but any text format such as sdf and csv will basically use the same algorithm.  The algorithm is as follows:

  1. Instantiate an EDI document object,

  2. Load the appropriate SEF file, which is essentially a schema of a Transaction Set so that FREDI would know how to parse the corresponding EDI file correctly.

  3. Load the EDI file into the EDI document object.

  4. Have a loop that traverses through the EDI file by segment, and write its data into a text file.

  5. Save and close text output.

    Private Sub cmdTranEdiXml_Click()
        Dim oEdiDoc As Fredi.ediDocument
        Dim oSchemas As Fredi.ediSchemas
        Dim oSegment As Fredi.ediDataSegment
        Dim oElement As Fredi.ediDataElement
        Dim oComposite As Fredi.ediDataElements
        Dim oSubElement As Fredi.ediDataElement
        Dim oXmlFile As Scripting.TextStream
        Dim oFs As Scripting.FileSystemObject
        Dim sSegmentId  As String
        Dim nElemCount As Integer
        Dim i As Integer
        Dim sPath As String
        sPath = App.Path & "\"
        Set oEdiDoc = New Fredi.ediDocument
        'Changing the cursor type to forward does not load the entire EDI file at once,
        'but a data segments at a time and disposing of them after being read, thus is more
        'memory effecient
        oEdiDoc.CursorType = Cursor_ForwardOnly
        'Disable internal library, and use SEF files only.
        Set oSchemas = oEdiDoc.GetSchemas
        oSchemas.EnableStandardReference = False
        'Load SEF file
        oEdiDoc.LoadSchema sPath & "850_4010.SEF", Schema_Standard_Exchange_Format
        'Load EDI File
        oEdiDoc.LoadEdi sPath & "850.X12"

        'create text file
        Set oFs = New Scripting.FileSystemObject
        Set oXmlFile = oFs.CreateTextFile(sPath & "XmlFile.xml", True)
        oXmlFile.Write "<?xml version=""1.0"" encoding=""ISO-8859-1"" ?>"

        'Write open tag for EDI X12 document
        oXmlFile.Write "<EDI-X12-4010>"
        'get first data segment in EDI file
        Set oSegment = oEdiDoc.FirstDataSegment
        'loop to traverse thru EDI file by segment
        Do While Not oSegment Is Nothing
            sSegmentId = oSegment.ID
            'Write open tag for control segments
            If sSegmentId = "ISA" Then
                oXmlFile.Write "<Interchange-Control>"
            ElseIf sSegmentId = "GS" Then
                oXmlFile.Write "<Functional-Group-Control>"
            ElseIf sSegmentId = "ST" Then
                oXmlFile.Write "<Transaction-Set-Control>"
            End If
            'Write open segment reference information
            oXmlFile.Write "<SegmentRef ID=""" & Trim(oSegment.ID) & """ Pos=""" _
			    & Trim(Str(oSegment.Position)) & """>"
            nElemCount = oSegment.Count
            For i = 1 To nElemCount
            Set oElement = oSegment.DataElement(i)
                'Write element information
                oXmlFile.Write "<Element Id=""" & oElement.ID & """ Pos=""" & Trim(Str(oElement.Position)) _
				    & """ Value=""" & Trim(oElement.Value) & """/>"
            'Write close tag for segment reference
            oXmlFile.Write "</SegmentRef>"
            'Write close tag for control segments
            If sSegmentId = "SE" Then
                oXmlFile.Write "</Transaction-Set-Control>"
            ElseIf sSegmentId = "GE" Then
                oXmlFile.Write "</Functional-Group-Control>"
            ElseIf sSegmentId = "IEA" Then
                oXmlFile.Write "</Interchange-Control>"
            End If
            'get next segment
            Set oSegment = oSegment.Next
        'Write close tag for EDI X12 document
        oXmlFile.Write "</EDI-X12-4010>"
        'end of text file
        MsgBox "Done"
    End Sub


Below is a partial image of the Text file output


The actual program can be downloaded here.

    Click here to evaluate the Framework EDI