任务:使用JavaCompute节点进行XML转化
一.如图建立Flow
二.编辑JavaCompute节点的Java代码如下:
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;
public class flow_JavaCompute extends MbJavaComputeNode {
public void evaluate(MbMessageAssembly inassembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
// MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage inMessage = inassembly.getMessage();
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly=new MbMessageAssembly(inassembly,outMessage);
MbElement root=outMessage.getRootElement();
MbElement rootElm=root.getLastChild().getFirstChild();
MbElement employee1=rootElm.createElementAsLastChild(MbElement.TYPE_NAME,"empoyee",null);
employee1.setValue("Andy");
MbElement employee2=rootElm.createElementAsLastChild(MbElement.TYPE_NAME,"empoyee",null);
employee2.setValue("Bill");
MbElement id = employee2.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE,"id", "221415");
out.propagate(outAssembly);
outMessage.clearMessage();
}
}
三.输入XML如下
<employees><employee>Hy</employee></employees>
四.输出XML如下:
<employees><employee>Hy</employee><empoyee>Andy</empoyee><empoyee id="221415">Bill</empoyee></employees>
注意这里面的代码操作与我们常见XML很不一样,一个是取根节点处,一个是给节点设置值处,一个是给节点增加属性处。
五.工程从这里下载
http://www.blogjava.net/Files/heyang/JavaCompute0922_01.rar