使用SMOOKS生成EDI文件

Shi*_*the 20 java xml edi smooks

如何使用SMOOKS将XML文件转换为EDI文件?

我能够将EDI转换为XML,实际上这是SMOOKS提供的示例的一部分.

sid*_*tha 1

根据你的问题,我尝试对此进行一些研究。请检查是否对您有帮助。

\n\n

这是要转换的源 edi 文件:

\n\n
HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006\nCUS*user1*Harry^Fletcher*SD\nORD*1*1*364*The 40-Year-Old Virgin*29.98\nORD*2*1*299*Pulp Fiction*29.99\n
Run Code Online (Sandbox Code Playgroud)\n\n

这就是我们转型的预期结果:

\n\n
<Order>\n    <header>\n            <order-id>1</order-id>\n            <status-code>0</status-code>\n            <net-amount>59.97</net-amount>\n            <total-amount>64.92</total-amount>\n            <tax>4.95</tax>\n            <date>Wed Nov 15 13:45:28 EST 2006</date>\n    </header>\n    <customer-details>\n            <username>user1</username>\n            <name>\n                    <firstname>Harry</firstname>\n                    <lastname>Fletcher</lastname>\n            </name>\n            <state>SD</state>\n    </customer-details>\n    <order-item>\n            <position>1</position>\n            <quantity>1</quantity>\n            <product-id>364</product-id>\n            <title>The 40-Year-Old Virgin</title>\n            <price>29.98</price>\n    </order-item>\n    <order-item>\n            <position>2</position>\n            <quantity>1</quantity>\n            <product-id>299</product-id>\n            <title>Pulp Fiction</title>\n            <price>29.99</price>\n    </order-item>\n</Order>\n
Run Code Online (Sandbox Code Playgroud)\n\n

Smooks 配置

\n\n

我们只需指定 SmooksEDIParser\xe2\x80\x89 作为流解析器。可以添加更多转换配置来进一步转换此消息。\n这里是配置(“smooks-config.xml”):

\n\n
<?xml version="1.0"?>\n<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"\n                  xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.1.xsd">\n\n<!--\nConfigure the EDI Reader to process the message stream into a stream of SAX events.\n-->\n<edi:reader mappingModel="/example/edi-to-xml-order-mapping.xml" />\n\n</smooks-resource-list>\n
Run Code Online (Sandbox Code Playgroud)\n\n

这是 edi 映射(“/src/main/java/example/edi-to-xml-order-mapping.xml”):

\n\n
<?xml version="1.0" encoding="UTF-8"?>\n<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.0.xsd">\n<medi:description name="DVD Order" version="1.0" />\n<medi:delimiters segment="&#10;" field="*" component="^" sub-component="~" />\n<medi:segments xmltag="Order">\n    <medi:segment segcode="HDR" xmltag="header">\n        <medi:field xmltag="order-id" />\n        <medi:field xmltag="status-code" />\n        <medi:field xmltag="net-amount" />\n        <medi:field xmltag="total-amount" />\n        <medi:field xmltag="tax" />\n        <medi:field xmltag="date" />\n    </medi:segment>\n    <medi:segment segcode="CUS" xmltag="customer-details">\n        <medi:field xmltag="username" />\n        <medi:field xmltag="name">\n            <medi:component xmltag="firstname" />\n            <medi:component xmltag="lastname" />\n        </medi:field>\n        <medi:field xmltag="state" />\n    </medi:segment>\n    <medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1">\n        <medi:field xmltag="position" />\n        <medi:field xmltag="quantity" />\n        <medi:field xmltag="product-id" />\n        <medi:field xmltag="title" />\n        <medi:field xmltag="price" />\n    </medi:segment>\n</medi:segments>\n</medi:edimap>\n
Run Code Online (Sandbox Code Playgroud)\n\n

执行转换:

\n\n
// Instantiate Smooks with the config...\nSmooks smooks = new Smooks("smooks-config.xml");\n\ntry {\n// Filter the input message to the outputWriter...\nsmooks.filterSource(new StreamSource(messageIn), new\nStreamResult(messageOut));\n} finally {\nsmooks.close();\n}\n
Run Code Online (Sandbox Code Playgroud)\n

  • 您在此处解释的这个示例将从 edi 文件创建 XML 文件。但是有人提出了关于从 XML 数据准备 EDI 文件的问题。您能否告诉我如何做到这一点。 (3认同)