XML to EDIFACT - programmaticaly configure edifact unparser

14 views
Skip to first unread message

Faress Shihada

unread,
Jun 22, 2023, 12:12:47 PM6/22/23
to Smooks Development
Hi, 

I'm trying to configure Smooks to parse XML to EDIFACT. I used the below xml configuration and it worked:
<?xml version="1.0"?>
<smooks-resource-list xmlns="https://www.smooks.org/xsd/smooks-2.0.xsd"
                      xmlns:edifact="https://www.smooks.org/xsd/smooks/edifact-2.0.xsd"
                      xmlns:core="https://www.smooks.org/xsd/smooks/smooks-core-1.6.xsd">

    <core:smooks filterSourceOn="/Interchange">
        <core:action>
            <core:inline>
                <core:replace/>
            </core:inline>
        </core:action>
        <core:config>
            <smooks-resource-list>
                <edifact:unparser schemaURI="/d03b/EDIFACT-Messages.dfdl.xsd" unparseOnNode="*">
                    <edifact:messageTypes>
                        <edifact:messageType>INVOIC</edifact:messageType>
                    </edifact:messageTypes>
                </edifact:unparser>
            </smooks-resource-list>
        </core:config>
    </core:smooks>

</smooks-resource-list>

However, I want do the same configuration programmatically. How is that possible? 
I tried the below code, not sure if I'm in the correct direction:

Smooks smooks = new Smooks();
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><D03B:Interchange xmlns:D03B=\"http://www.ibm.com/dfdl/edi/un/edifact/D03B\" xmlns:srv=\"http://www.ibm.com/dfdl/edi/un/service/4.1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><UNB><S001><E0001>UNOA</E0001><E0002>4</E0002></S001><S002><E0004>LOCK</E0004><E0007>02</E0007></S002><S003><E0010>CBP-ACE-TEST</E0010><E0007>02</E0007></S003><S004><E0017>41013</E0017><E0019>1901</E0019></S004><E0020>16</E0020><S005><E0022></E0022></S005><E0026>ACE</E0026></UNB><FunctionGroup><UNG><E0038>PAXLST</E0038><S006><E0040>LOCK</E0040><E0007>02</E0007></S006><S007><E0044>CBP-ACE-TEST</E0044><E0007>02</E0007></S007><S004><E0017>41013</E0017><E0019>1901</E0019></S004><E0048>16</E0048><E0051>UN</E0051><S008><E0052>D</E0052><E0054>03B</E0054></S008></UNG><D03B:Message><UNH><E0062>PAXLST16</E0062><S009><E0065>PAXLST</E0065><E0052>D</E0052><E0054>03B</E0054><E0051>UN</E0051></S009></UNH><D03B:PAXLST><BGM><C002><E1001>10</E1001></C002><C106><E1004>LOCKKH04103101</E1004></C106><E1225>4</E1225></BGM><RFF><C506><E1153>RFA</E1153><E1154>03</E1154></C506></RFF><SegGrp-2><TDT><E8051>11</E8051><C040><E3127>LOCK</E3127><E1131>172</E1131></C040></TDT><DTM><C507><E2005>132</E2005><E2380>20041230</E2380><E2379>102</E2379></C507></DTM></SegGrp-2><SegGrp-4><NAD><E3035>VW</E3035><C082><E3039>0000041153</E3039><E1131>109</E1131></C082><C080><E3036>AOMAD</E3036><E3036>CHRIS</E3036><E3045>1</E3045></C080><C059><E3042>11107 SUNSET HILLS ROAD</E3042></C059><E3164>RESTON</E3164><C819><E3229>VA</E3229><E1131>163</E1131><E3055>5</E3055></C819><E3251>20190</E3251></NAD><ATT><E9017>2</E9017><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956><C956><E9019>M</E9019></C956></ATT><DTM><C507><E2005>329</E2005><E2380>19350919</E2380><E2379>102</E2379></C507></DTM><EMP><E9003>4</E9003><C950><E9007>1</E9007></C950><E9035>8</E9035></EMP><NAT><E3493>2</E3493><C042><E3293>US</E3293><E3055>5</E3055></C042></NAT><SegGrp-5><DOC><C002><E1001>5K</E1001></C002><C503><E1004>P100971204141</E1004></C503></DOC><LOC><E3227>91</E3227><C517><E3225>VA</E3225><E1131>163</E1131></C517></LOC><LOC><E3227>91</E3227><C517><E3225>US</E3225><E1131>162</E1131></C517></LOC></SegGrp-5><SegGrp-5><DOC><C002><E1001>39</E1001></C002><C503><E1004>15504141</E1004></C503></DOC><LOC><E3227>91</E3227><C517><E3225>US</E3225><E1131>162</E1131></C517></LOC></SegGrp-5></SegGrp-4></D03B:PAXLST><UNT><E0074>16</E0074><E0062>PAXLST16</E0062></UNT></D03B:Message><UNE><E0060>1</E0060><E0048>16</E0048></UNE></FunctionGroup><UNZ><E0036>1</E0036><E0020>16</E0020></UNZ></D03B:Interchange>";
        DfdlSchema dfdlSchema = new DfdlSchema(new URI("/d03b/EDIFACT-Messages.dfdl.xsd"), new HashMap<String, String>(){{
            this.put("{http://www.ibm.com/dfdl/EDI/Format}SegmentTerm", "'");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}FieldSep", "+");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}CompositeSep", ":");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}EscapeChar", "?");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}RepeatSep", "*");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}DecimalSep", ".");
            this.put("{http://www.ibm.com/dfdl/EDI/Format}GroupingSep", ",");
        }}, ValidationMode.Full, false, false, null);
        try {
            DfdlUnparser dfdlUnparser = new DfdlUnparser(dfdlSchema.compile());
//             this is required because of a bug in DfdlUnparser
            ResourceConfig resourceConfig = new DefaultResourceConfig();
            resourceConfig.setParameter("schemaURI", "");
            smooks.addConfiguration(resourceConfig);
            smooks.getApplicationContext().getRegistry().lookup(new LifecycleManagerLookup())
                    .applyPhase(dfdlUnparser, new PostConstructLifecyclePhase(
                            new Scope(smooks.getApplicationContext().getRegistry(), resourceConfig,
                                    dfdlUnparser)));
/////////////////////////////////////////////////
            smooks.addVisitor(dfdlUnparser, "Interchange");
            smooks.setFilterSettings(FilterSettings.newSaxNgSettings().setDefaultSerializationOn(false));
        } catch (Throwable e) {
            Log.a(this.getClass()).info(e.toString());
        }
        StringResult stringResult = new StringResult();
//        smooks.filterSource(new StreamSource(new ByteArrayInputStream(input.get("stream").toString().getBytes())), stringResult);
        smooks.filterSource(new StreamSource(new ByteArrayInputStream(xml.getBytes())), stringResult);
        output.put("result", stringResult.getResult());


I'm getting the below error:
Failed to filter source class org.apache.daffodil.japi.DaffodilUnparseErrorSAXException Cause: Unparse Error: Expected element start event for D03B:{http://www.ibm.com/dfdl/edi/un/edifact/D03B}RESMSG, but received element start event for (invalid) {http://www.ibm.com/dfdl/edi/un/edifact/D03B}Interchange.
Data location was preceding byte 0

Claude

unread,
Jul 22, 2023, 3:42:53 AM7/22/23
to Smooks Development
Please post your question to the Smooks user group. This group is intended for Smooks development topics.

Claude
Reply all
Reply to author
Forward
0 new messages