Skip to Content

This an option to easy read an EDI 850 File,

In this case you need no to implement a complex java to read the file.

When you need to deal with EDI 850 Purchase order. If there are no option to get SAP B2B Addon.

You don’t need to write a complex code to read the segments and structures.

This is the easy way, you use this class to get an XML with the information of an EDI File, then you can use message mapping, and the set of powerful tools provided by SAP Process Orchestration.

 

This is a sample using com.berryworks.edireader.EDIReader.

 

From EDI to XML using Java Mapping

Why to Write Java Map:

In one client projects there was few inbound EDI interface, then client like not to invest in SAP B2B Addon. Then java map is used to achieve EDI to XML.

 

 

This is the imput file sample:

ISA*00* *00* *ZZ*0011223456 *ZZ*999999999 *990320*0157*U*00300*000000015*0*P*~$
GS*PO*0011223456*999999999*950120*0147*5*X*003040$
ST*850*000000001$
BEG*00*SA*95018017***950118$
N1*SE*UNIVERSAL WIDGETS$
N3*375 PLYMOUTH PARK*SUITE 205$
N4*IRVING*TX*75061$
N1*ST*JIT MANUFACTURING$
N3*BUILDING 3B*2001 ENTERPRISE PARK$
N4*JUAREZ*CH**MEX$
N1*AK*JIT MANUFACTURING$
N3*400 INDUSTRIAL PARKWAY$
N4*INDUSTRIAL AIRPORT*KS*66030$
N1*BT*JIT MANUFACTURING$
N2*ACCOUNTS PAYABLE DEPARTMENT$
N3*400 INDUSTRIAL PARKWAY$
N4*INDUSTRIAL AIRPORT*KS*66030$
PO1*001*4*EA*330*TE*IN*525*VN*X357-W2$
PID*F****HIGH PERFORMANCE WIDGET$
SCH*4*EA****002*950322$
CTT*1*1$
SE*20*000000001$
GE*1*5$
IEA*1*000000015$

 

This is the Output XML sample:

 

<?xml version="1.0" encoding="UTF-8"?>
<ediroot xmlns="http://www.berryworkssoftware.com/2008/edireader">
	<interchange Standard="ANSI X.12" Date="990320" Time="0157" StandardsId="U" Version="00300" Control="000000015">
		<sender>
			<address Id="0011223456     " Qual="ZZ"/>
		</sender>
		<receiver>
			<address Id="999999999      " Qual="ZZ"/>
		</receiver>
		<group GroupType="PO" ApplSender="0011223456" ApplReceiver="999999999" Date="950120" Time="0147" Control="5" StandardCode="X" StandardVersion="003040">
			<transaction DocType="850" Name="Purchase Order" Control="000000001">
				<segment Id="BEG">
					<element Id="BEG01">00</element>
					<element Id="BEG02">SA</element>
					<element Id="BEG03">95018017</element>
					<element Id="BEG06">950118</element>
				</segment>
				<loop Id="N1">
					<segment Id="N1">
						<element Id="N101">SE</element>
						<element Id="N102">UNIVERSAL WIDGETS</element>
					</segment>
					<segment Id="N3">
						<element Id="N301">375 PLYMOUTH PARK</element>
						<element Id="N302">SUITE 205</element>
					</segment>
					<segment Id="N4">
						<element Id="N401">IRVING</element>
						<element Id="N402">TX</element>
						<element Id="N403">75061</element>
					</segment>
				</loop>
				<loop Id="N1">
					<segment Id="N1">
						<element Id="N101">ST</element>
						<element Id="N102">JIT MANUFACTURING</element>
					</segment>
					<segment Id="N3">
						<element Id="N301">BUILDING 3B</element>
						<element Id="N302">2001 ENTERPRISE PARK</element>
					</segment>
					<segment Id="N4">
						<element Id="N401">JUAREZ</element>
						<element Id="N402">CH</element>
						<element Id="N404">MEX</element>
					</segment>
				</loop>
				<loop Id="N1">
					<segment Id="N1">
						<element Id="N101">AK</element>
						<element Id="N102">JIT MANUFACTURING</element>
					</segment>
					<segment Id="N3">
						<element Id="N301">400 INDUSTRIAL PARKWAY</element>
					</segment>
					<segment Id="N4">
						<element Id="N401">INDUSTRIAL AIRPORT</element>
						<element Id="N402">KS</element>
						<element Id="N403">66030</element>
					</segment>
				</loop>
				<loop Id="N1">
					<segment Id="N1">
						<element Id="N101">BT</element>
						<element Id="N102">JIT MANUFACTURING</element>
					</segment>
					<segment Id="N2">
						<element Id="N201">ACCOUNTS PAYABLE DEPARTMENT</element>
					</segment>
					<segment Id="N3">
						<element Id="N301">400 INDUSTRIAL PARKWAY</element>
					</segment>
					<segment Id="N4">
						<element Id="N401">INDUSTRIAL AIRPORT</element>
						<element Id="N402">KS</element>
						<element Id="N403">66030</element>
					</segment>
				</loop>
				<loop Id="PO1">
					<segment Id="PO1">
						<element Id="PO101">001</element>
						<element Id="PO102">4</element>
						<element Id="PO103">EA</element>
						<element Id="PO104">330</element>
						<element Id="PO105">TE</element>
						<element Id="PO106">IN</element>
						<element Id="PO107">525</element>
						<element Id="PO108">VN</element>
						<element Id="PO109">X357-W2</element>
					</segment>
					<loop Id="PID">
						<segment Id="PID">
							<element Id="PID01">F</element>
							<element Id="PID05">HIGH PERFORMANCE WIDGET</element>
						</segment>
					</loop>
					<loop Id="SCH">
						<segment Id="SCH">
							<element Id="SCH01">4</element>
							<element Id="SCH02">EA</element>
							<element Id="SCH06">002</element>
							<element Id="SCH07">950322</element>
						</segment>
					</loop>
				</loop>
				<loop Id="CTT">
					<segment Id="CTT">
						<element Id="CTT01">1</element>
						<element Id="CTT02">1</element>
					</segment>
				</loop>
			</transaction>
		</group>
	</interchange>
</ediroot>

 

+++Important you need an aditional JAR: edireader-4.7.3.jar, it is esasy to find on Internet.

This is the source code for Java Mapping:



import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;


import com.berryworks.edireader.EDIReader;
import com.berryworks.edireader.error.EDISyntaxExceptionHandler;
import com.berryworks.edireader.error.RecoverableSyntaxException;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class EdiTransformer extends AbstractTransformation {

	boolean namespaceEnabled=true;
	boolean recover=true;
	// @Owverride
	public void transform(TransformationInput arg0, TransformationOutput arg1)
			throws StreamTransformationException {
		
		this.execute(arg0.getInputPayload().getInputStream(), arg1
				.getOutputPayload().getOutputStream());
		
	}

	
	public void execute(InputStream in, OutputStream out) {

		
        try {
            XMLReader ediReader = new EDIReader();

            // Tell the ediReader if an xmlns="http://..." is desired
            if (namespaceEnabled) {
                ((EDIReader) ediReader).setNamespaceEnabled(namespaceEnabled);
            }

            // Tell the ediReader to handle EDI syntax errors instead of aborting
            if (recover) {
                ((EDIReader) ediReader).setSyntaxExceptionHandler(new IgnoreSyntaxExceptions());
            }

            // Establish the SAXSource
            //Reader inputReader = in.;
            InputSource inputSource = new InputSource(in);
            //getTrace().addWarning("prueba "+inputSource.toString());
            SAXSource source = new SAXSource(ediReader, inputSource);

            // Establish a Transformer
            Transformer transformer = TransformerFactory.newInstance().newTransformer();

            // Use a StreamResult to capture the generated XML output
            StreamResult result = new StreamResult(out);

            // Call the Transformer to generate XML output from the parsed input
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            System.err.println("\nUnable to create Transformer: " + e);
        } catch (TransformerException e) {
            System.err.println("\nFailure to transform: " + e);
            System.err.println(e.getMessage());
        }

        try {
            in.close();
        } catch (IOException ignored) {
        }
        try {
            out.close();
        } catch (IOException ignored) {
        }		
		
	}
	
	static public void main(String[] args) {
		EdiTransformer parser = new EdiTransformer();
		try {
			FileInputStream in = new FileInputStream("d:\\edi_file_in.edi");
			FileOutputStream out = new FileOutputStream("d:\\xml_file_out.xml");
			parser.execute(in, out);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	
	
    static class IgnoreSyntaxExceptions implements EDISyntaxExceptionHandler {

        public boolean process(RecoverableSyntaxException syntaxException) {
            System.out.println("Syntax Exception. class: " + syntaxException.getClass().getName() + "  message:" + syntaxException.getMessage());
            // Return true to indicate that you want parsing to continue.
            return true;
        }
    }
}

 

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply