gpt4 book ai didi

java - Smooks:从 Java 输出 EDI

转载 作者:行者123 更新时间:2023-11-30 08:11:20 32 4
gpt4 key购买 nike

受到 Smooks EDI writer 中提出的解决方案的启发,我有以下 Java 代码:

// Creates minimal EDI message with one field populated
EdifactV3D98AMEDPID edi = new EdifactV3D98AMEDPID();
UNBInterchangeHeader header = new UNBInterchangeHeader();
UNBInterchangeHeader.S002SenderIdentification s002SenderIdentification = new UNBInterchangeHeader.S002SenderIdentification();
s002SenderIdentification.setE0004SenderIdentification("TEST");
header.setS002SenderIdentification(s002SenderIdentification);
edi.setUNBInterchangeHeader(header);

Smooks smooks = new Smooks("edi-output-smooks-config.xml");

// Sets up access to exports specified in Smooks config
ExecutionContext executionContext = smooks.createExecutionContext();
Exports exports = Exports.getExports(smooks.getApplicationContext());
Result[] results = exports.createResults();

smooks.filterSource(executionContext, new JavaSource(edi), results);

List<Object> objects = Exports.extractResults(results, exports);
JavaResult.ResultMap map = (JavaResult.ResultMap) objects.get(0);

D98AInterchangeFactory factory = D98AInterchangeFactory.getInstance();
UNEdifactInterchange41 unEdifactInterchange = (UNEdifactInterchange41) map.get("unEdifactInterchange");

// Should output EDI message as String, but StringWriter is empty
StringWriter ediOutStream = new StringWriter();
factory.toUNEdifact(unEdifactInterchange, ediOutStream);

...使用以下 Smooks 配置:

<?xml version="1.0" encoding="UTF-8"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

<!-- Imports D98A Edifact binding found in Maven dependency -->
<import file="/org/milyn/edi/unedifact/d98a/message-bindingconfig.xml" />
<import file="/org/milyn/smooks/edi/unedifact/model/r41/bindings/unedifact-interchange.xml" />

<!-- Configures Result object for accessing EDI output -->
<core:exports>
<core:result type="org.milyn.payload.JavaResult"/>
</core:exports>

</smooks-resource-list>

StringWriter 返回一个空字符串,而我曾希望/期望 Java 对象作为 EDI 字符串。

有什么建议或指示吗?

最佳答案

我找不到类 EdifactV3D98AMEDPID、UNBInterchangeHeade。但是我做了一个类似的测试:

UNEdifactInterchange41 edi = new UNEdifactInterchange41();
UNB41 header = new UNB41();
header.setSender(null);
Party sender = new Party();
sender.setInternalId("TEST");
header.setSender(sender);
edi.setInterchangeHeader(header);

Smooks smooks = new Smooks("edi-output-smooks-config.xml");

ExecutionContext executionContext = smooks.createExecutionContext();
Exports exports = Exports.getExports(smooks.getApplicationContext());
Result[] results = exports.createResults();

smooks.filterSource(executionContext, new JavaSource(edi), results);

List<Object> objects = Exports.extractResults(results, exports);
JavaResult.ResultMap map = (JavaResult.ResultMap) objects.get(0);

D98AInterchangeFactory factory = D98AInterchangeFactory.getInstance();
UNEdifactInterchange41 u = (UNEdifactInterchange41) map.get("unEdifactInterchange");

// Should output EDI message as String, but StringWriter is empty
StringWriter ediOutStream1 = new StringWriter();
factory.toUNEdifact(u, ediOutStream1);

我得到了相同的结果,最后是一个空字符串。

但我认为问题出在您对 smook 的使用上。我只将 smooks 与 Apache Camel 一起使用,所以我不知道这是否是在 smooks 中进行转换的正确方法,但我认为在这种情况下你不必调用 smook,你的输入是一个对象 EDI,你想要以 EDI 格式打印您的对象。

你唯一需要做的就是:

UNEdifactInterchange41 edi = new UNEdifactInterchange41();
UNB41 header = new UNB41();
header.setSender(null);
Party sender = new Party();
sender.setInternalId("TEST");
header.setSender(sender);
edi.setInterchangeHeader(header);

D98AInterchangeFactory factory = D98AInterchangeFactory.getInstance();

StringWriter ediOutStream1 = new StringWriter();
factory.toUNEdifact(edi, ediOutStream1);

我已经测试过了,你会得到这个:

UNB++::TEST'

希望对你有帮助

关于java - Smooks:从 Java 输出 EDI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31069450/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com