java - 使用 SMOOKS 生成 EDI 文件

标签 java xml edi smooks

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

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

最佳答案

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

下面是要转换的源 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

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

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

Smooks 配置

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

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                  xmlns:edi="http://www.milyn.org/xsd/smooks/edi-1.1.xsd">

<!--
Configure the EDI Reader to process the message stream into a stream of SAX events.
-->
<edi:reader mappingModel="/example/edi-to-xml-order-mapping.xml" />

</smooks-resource-list>

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

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

执行转换:

// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");

try {
// Filter the input message to the outputWriter...
smooks.filterSource(new StreamSource(messageIn), new
StreamResult(messageOut));
} finally {
smooks.close();
}

关于java - 使用 SMOOKS 生成 EDI 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427887/

相关文章:

java - 在 swing jtable 中的行数之后插入行或记录

java - 如何在多对多关联表上编写查询

java - 在 Jackson/Jersey JAVA 上发布带有多个参数 JSON 和 String 的请求

java - 第一次使用 JAXB 将 XML 映射到 Java 类 — 需要一些构建帮助

python - 如何使用 xml.dom.minidom 查找 sibling ?

edi - EDI ISA 段的正确填充

edi - 865 与 997 确认文件

java - 我可以让 Jenkins 每小时检查一次 SVN 是否有变化吗?

java.util.InvalidPropertiesFormatException : SAXParseException

azure - 使用 856 源循环 Functoid