java smooks,Smooks:从Java输出EDI

Inspired by the proposed solution in Smooks EDI writer, I have the following Java code:

// 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 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);

... with the following Smooks config:

xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd">

The StringWriter returns an empty String, whereas I had hoped/expected the Java object as an EDI String.

Any suggestions or pointers?

解决方案

I don't find the classes EdifactV3D98AMEDPID, UNBInterchangeHeade. But i made a similar test:

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 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);

I had the same result, an empty string in the end.

But i think the problem is your use of smooks. I only used smooks with Apache Camel, so i don't know if that is the correct way to make a transformation in smooks, but i think that in this case you dont have to call smook, your input is an object EDI and you want to print you object in EDI Format.

The only thing you have to do is:

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);

I already tested, and you get this:

UNB++::TEST'

I hope that this could help you

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值