Edi to Xml in smooks 2.2.0

68 views
Skip to first unread message

Ashish Singh

unread,
Jan 30, 2025, 11:53:57 AMJan 30
to Smooks Users
Hi Team,
I am new to smooks.I was able to convert edi file to xml using the example provided in github repository.But i have been struggling to do the same by externalising properties like segmentdelimiter ,terminator etc .I want to set these properties from java code any reference to code would really help me out .
Thank you


Giovanni Licata

unread,
Jan 30, 2025, 12:08:55 PMJan 30
to smook...@googlegroups.com
Hi
I think you should look for something about EDIReaderConfigurator to programmatically configure the reader

--
You received this message because you are subscribed to the Google Groups "Smooks Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to smooks-user...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/smooks-user/2419ce55-0b9f-4f75-a64e-0b26de79de3fn%40googlegroups.com.

Ashish Singh

unread,
Jan 30, 2025, 1:00:50 PMJan 30
to Smooks Users
Thank you for quick response.
I tried using EdiReaderConfigurator.But it throws weird exception.The same xsd and schemaconfig.xml and input message work fine if I use the code below.

Smooks smooks = new Smooks(new DefaultApplicationContextBuilder().build());
smooks.addResourceConfigs("edi-to-xml/smooks-config.xml");
StringSink sink = new StringSink();
smooks.filterSource(new StreamSource<>(new ByteArrayInputStream(message)), sink);
return sink.getResult();

But fails for :

Smooks smooks = new Smooks();
EdiReaderConfigurator ediReaderConfigurator = new EdiReaderConfigurator("edi-to-xml/src/main/resources/edi-to-xml-order-mapping.dfdl.xsd");
ediReaderConfigurator.setDataElementSeparator("*");
ediReaderConfigurator.setSegmentTerminator("~%NL;");
ediReaderConfigurator.setValidationMode(ValidationMode.Full);
smooks.addResourceConfig(ediReaderConfigurator.toConfig().get(0));
StringSink sink = new StringSink();
smooks.filterSource(new StreamSource<>(new ByteArrayInputStream(message)), sink);

Exception::
org.smooks.api.SmooksException: Failed to filter source
at org.smooks.engine.delivery.sax.ng.SaxNgFilter.doFilter(SaxNgFilter.java:123)
at org.smooks.engine.delivery.sax.ng.SaxNgFilter.doFilter(SaxNgFilter.java:97)
at org.smooks.Smooks._filter(Smooks.java:570)
at org.smooks.Smooks.filterSource(Smooks.java:512)
at org.smooks.Smooks.filterSource(Smooks.java:491)
at org.smooks.examples.edi2xml.Main.runSmooksTransform(Main.java:111)
at org.smooks.examples.edi2xml.Main.main(Main.java:132)
Caused by: org.smooks.cartridges.dfdl.parser.ParserDfdlSmooksException: Parse Error: Found enclosing delimiter: '*' during scan for local delimiter(s): 'ISA', '~%NL;'.
 The expected delimiter(s) were:
  initiator 'ISA' from ISA_InterchangeControlHeader Location line 20 column 22 in C:\repositories\smooks-examples\edi-to-xml\src\main\resources\edi-to-xml-order-mapping.dfdl.xsd.
  terminator '~%NL;' from ISA_InterchangeControlHeader Location line 20 column 22 in C:\repositories\smooks-examples\edi-to-xml\src\main\resources\edi-to-xml-order-mapping.dfdl.xsd..
 The enclosing delimiter was from sequence[1] Location line 18 column 10 in C:\repositories\smooks-examples\edi-to-xml\src\main\resources\edi-segment-definition.xsd.


Attached sample files.
input-message.edi
edi-to-xml-order-mapping.dfdl.xsd
smooks-config.xml

Giovanni Licata

unread,
Jan 30, 2025, 3:24:35 PMJan 30
to smook...@googlegroups.com
I'm not an expert but I think that compared to the smook-config.xml configuration file you should:
- remove the line ediReaderConfigurator.setValidationMode(ValidationMode.Full);
- add ediReaderConfigurator.setCompositeDataElementSeparator("^")
Here you can see what I think are the default parameters of the reader https://github.com/smooks/smooks-edi-cartridge/blob/master/edi-cartridge/src/main/java/org/smooks/cartridges/edi/EdiReaderConfigurator.java.
For the DFDL parser the validation should be disabled by default

Giovanni Licata

unread,
Jan 30, 2025, 4:30:32 PMJan 30
to smook...@googlegroups.com
Hi
starting from xml-to-edi example editing Main.java as below  works as aspected. Share your imported XSD and I can check exactly your configuration.

...
  protected static String runSmooksTransform() throws IOException, SAXException, SmooksException {
        // Instantiate Smooks with the config...
        Smooks smooks = new Smooks(new DefaultApplicationContextBuilder().withClassLoader(Main.class.getClassLoader()).build());
        EdiReaderConfigurator ediReaderConfigurator = new EdiReaderConfigurator("edi-to-xml-order-mapping.dfdl.xsd");
        ediReaderConfigurator.setDataElementSeparator("*");
        ediReaderConfigurator.setSegmentTerminator("%NL;");
        ediReaderConfigurator.setCompositeDataElementSeparator("^");
       
        smooks.setReaderConfig(ediReaderConfigurator);      
        //smooks.addResourceConfig(ediReaderConfigurator.toConfig().get(0));
        //smooks.addResourceConfigs("smooks-config.xml");
       ....



Ashish Singh

unread,
Jan 30, 2025, 5:58:55 PMJan 30
to Smooks Users
Thanks for checking, Attaching xsd files being used. Tried removing validation mode and adding  setCompositeDataElementSeparator still faced issue . 
edi-segment-definition.xsd
edi-to-xml-order-mapping.dfdl.xsd
input-message.edi
smooks-config.xml

Giovanni Licata

unread,
Jan 31, 2025, 1:34:39 AMJan 31
to smook...@googlegroups.com
Have you already tried programmatically cose with the sample configutation and input message before try your configutation? 

Giovanni Licata

unread,
Jan 31, 2025, 4:37:36 AMJan 31
to smook...@googlegroups.com
using your configuration works ... check output console in attachment.
Below the edited runSmooksTransform() 
Note that I use  smooks.setReaderConfig(ediReaderConfigurator) and not smooks.addResourceConfig() and setSegmentTerminator("%NL;") not setSegmentTerminator("~%NL;");

    protected static String runSmooksTransform() throws IOException, SAXException, SmooksException {
        // Instantiate Smooks with the config...
        Smooks smooks = new Smooks(new DefaultApplicationContextBuilder().withClassLoader(Main.class.getClassLoader()).build());
        EdiReaderConfigurator ediReaderConfigurator = new EdiReaderConfigurator("edi-to-xml-order-mapping.dfdl.xsd");
        ediReaderConfigurator.setDataElementSeparator("*");
        ediReaderConfigurator.setSegmentTerminator("%NL;");
        ediReaderConfigurator.setCompositeDataElementSeparator("^");
       
        smooks.setReaderConfig(ediReaderConfigurator);      
        //smooks.addResourceConfig(ediReaderConfigurator.toConfig().get(0));
        //smooks.addResourceConfigs("smooks-config.xml");
        try {
             // Create an exec context - no profiles....
            ExecutionContext executionContext = smooks.createExecutionContext();


            StringSink sink = new StringSink();

            // Configure the execution context to generate a report...
            executionContext.getContentDeliveryRuntime().addExecutionEventListener(new HtmlReportGenerator("target/report/report.html", executionContext.getApplicationContext()));

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

            return sink.getResult();
        } finally {
            smooks.close();
        }
    }

Giovanni Licata

unread,
Jan 31, 2025, 4:39:12 AMJan 31
to smook...@googlegroups.com
the attachment
output.txt

Ashish Singh

unread,
Jan 31, 2025, 2:09:29 PMJan 31
to Smooks Users
Thanks for looking into the issue . Somehow i overlooked the delimiter. 
Its working fine after making the change,
Reply all
Reply to author
Forward
0 new messages