2016-09-28 69 views
0

过去几个月来,我一直在研究Java模块以转换XML。例如,它应该接受一个soap请求并使用元数据存储库中的其他元素填充soap:header元素。该模块应该普遍适用于任何中间件(我的本机系统是SAP PI)。IBM Integration Bus Java计算节点:输出w3c.dom.Document或字符串

现在我的任务是将此模块作为jar实现到IBM Integration Bus中的JavaCompute节点中。问题是导出生成的XML我需要将数据导入JavaCompute节点的outMessage。但是,我没有找到将org.w3c.com.Document转换为MbElement或将文档或其内容插入MbElement的方法。

实际上,我没有看到任何方法可以根据需要使用IBM API,所以我必须编写代码读取已经完成的Document并构建MbElement(甚至不是XML字符串)从中。

这看起来如下:

public void evaluate(MbMessageAssembly inAssembly) throws MbException { 

    MbOutputTerminal out = getOutputTerminal("out"); 
    MbOutputTerminal alt = getOutputTerminal("alternate"); 

    MbMessage inMessage = inAssembly.getMessage(); 

    // create new empty message 
    MbMessage outMessage = new MbMessage(); 
    MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly, 
      outMessage); 

    try { 
     // optionally copy message headers 
     // copyMessageHeaders(inMessage, outMessage); 
     // ---------------------------------------------------------- 
     // Add user code below 

     //Create an example output Document 
     String outputContent = "<element><subelement>Value</subelement></element>"; 
     DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     InputSource is = new InputSource(); 
     is.setCharacterStream(new StringReader(outputContent)); 
     Document outDocument = db.parse(is); 

//Get the Document or its content into the outRoot or outMessage somehow. 

MbElement outRoot = outMessage.getRootElement(); 

//Start to iterate over the Document and use Methods like this to build up the MbElement? 
MbElement outBody = outRoot.createElementAsLastChild("request"); 

// End of user code 
} catch (MbException e) { ... 
+0

你有什么问题吗?您是正确的,只能通过IBM API访问传出消息树。 –

+0

问题是如果我能以某种方式自己不这样做,并将DOM转换为outMessage可以处理的东西以及如何将它放入消息中。 siarheib可能有正确的答案,我正在测试它的ATM。 –

回答

0

你可以投你org.w3c.com.Document以字节数组(example)。然后你可以使用下面的代码:

 MbMessage outMessage = new MbMessage(); 
     //copy message headers if required 
     MbElement oRoot = outMessage.getRootElement(); 
     MbElement oBody = oRoot.createElementAsLastChild(MbBLOB.PARSER_NAME); 
     oBody.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "BLOB", yourXmlAsByteArray); 
     MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly, inAssembly.getLocalEnvironment(), inAssembly.getExceptionList(), outMessage); 
+0

有趣的是,我会试试看看下一个节点如何处理BLOB对象。 Afaik虽然是一种常见的内容格式。 –

+0

正确答案。对于将来的读者:BLOB可以用于像“普通”xml这样的后续节点,我已经将它直接放到FileOutputNode中,它就像我在Code中生成它一样。 –

+0

只是一小部分。如果要将以下节点中的消息用作XML(换句话说,使用Integration Bus功能浏览消息结构),则可以使用ResetContentDescriptor节点将BLOB重新分解为XMLNSC。 – siarheib