smooks数据转换:edi to java

  • edi源文件内容:

    HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006
    CUS*user1*Harry^Fletcher*SD
    ORD*1*1*364*The 40-Year-Old Virgin*29.98
    ORD*2*1*299*Pulp Fiction*29.99
  • edi-to-java-mapping文件内容:

































  • 建立java project ,import edi源文件及mapping文件,根据以上文件可建立java class分别为:Coustomer,Header,Order,OrderItem.在smooks的example文件里有源码。导入文件在src文件夹下,创建smooks文件,点击input task——>add——>browse workspace——>edi源文件——>确定。添加映射关系文件,mapping model处选择browse——>导入在project下的mapping xml文件。
  • 右击input mask——>add tasking——>java mapping,在selected task details区右击——>add——>java class,依次添加相应的class并建立关系。如图所示:smooks数据转换:edi to java_第1张图片
  • 测试并转换,建立main class。(在测试之前需将smooks/lib下所以的jar包导入,方法:右击project——>build path——>add axternal archives……——>……,导入完毕。)main源码如下:

    import org.milyn.*;
    import org.milyn.payload.JavaResult;
    import org.milyn.container.*;
    import org.milyn.event.report.*;
    import org.milyn.io.*;
    import org.xml.sax.*;


    import javax.xml.transform.stream.*;
    import java.io.*;
    import java.util.*;


    public class Main {


        private static byte[] messageIn = readInputMessage();


        private final Smooks smooks;


        protected Main() throws IOException, SAXException {
            // Instantiate Smooks with the config...
            smooks = new Smooks("smooks-config.xml");
        }


        protected JavaResult runSmooksTransform(ExecutionContext executionContext) throws IOException, SAXException, SmooksException {
        try {
                Locale defaultLocale = Locale.getDefault();
                Locale.setDefault(new Locale("en", "IE"));


                org.milyn.payload.JavaResult javaResult = new org.milyn.payload.JavaResult();


                // Configure the execution context to generate a report...
                executionContext.setEventListener(new HtmlReportGenerator("target/report/report.html"));


                // Filter the input message to the outputWriter, using the execution context...
                smooks.filterSource(executionContext, new StreamSource(new ByteArrayInputStream(messageIn)), javaResult);


                Locale.setDefault(defaultLocale);


                return javaResult;
            } finally {
                smooks.close();
            }
        }


        public static void main(String[] args) throws IOException, SAXException, SmooksException {
            System.out.println("\n\n==============Message In==============");
            System.out.println(new String(messageIn));
            System.out.println("======================================\n");


            pause("The EDI input stream can be seen above.  Press 'enter' to see how this stream is transformed the Order Object graph...");


            Main smooksMain = new Main();
            ExecutionContext executionContext = smooksMain.smooks.createExecutionContext();
            org.milyn.payload.JavaResult result = smooksMain.runSmooksTransform(executionContext);




            System.out.println("\n==============EDI as Java Object Graph=============");
            System.out.println(result.getBean("order"));
            System.out.println("======================================\n\n");


            pause("And that's it!  Press 'enter' to finish...");
        }


        private static byte[] readInputMessage() {
            try {
                return StreamUtils.readStream(new FileInputStream("src/input-message.edi"));
            } catch (IOException e) {
                e.printStackTrace();
                return "".getBytes();
            }
        }


        private static void pause(String message) {
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                System.out.print("> " + message);
                in.readLine();
            } catch (IOException e) {
            }
            System.out.println("\n");
        }


        public org.milyn.payload.JavaResult runSmooksTransform() throws IOException, SAXException {
            ExecutionContext executionContext = smooks.createExecutionContext();
            return runSmooksTransform(executionContext);
        }
    }
  • 运行main class,成功后在/target、文件夹下可查看生产的xml内容验证文件,看是否是达到预期效果。
  • edi to java数据转换成功。smooks数据转换:edi to java_第2张图片

你可能感兴趣的:(数据转换)