Author |
Message
|
anandsitha |
Posted: Fri Jul 29, 2011 3:06 am Post subject: How to Create New XML Element Using JCompute Node? |
|
|
Acolyte
Joined: 26 Jul 2011 Posts: 59
|
Hi -
My msgflow would be FileInput Node--->Java Compute Node--->File Output Node.
I am sending an XML file with English and Maths Element. Now I want to compute total and get it in Output File with new Total Element. Could anyone help me in this suituation. I have tried with
MbElement Total = Student.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Total", "88") but it is not working.
What is comming in the input file is going to the output.
Compute Node Code:
import com.ibm.broker.javacompute.MbJavaComputeNode;
import com.ibm.broker.plugin.*;
public class SimpleComputeMsgFlow_JavaCompute extends MbJavaComputeNode {
public void evaluate(MbMessageAssembly assembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage inMessage = assembly.getMessage();
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new MbMessageAssembly(assembly,
outMessage);
try
{
MbElement root = outMessage.getRootElement();
//MbElement Student = root.getLastChild().getFirstChild();
//MbElement Rollno = Student.getFirstChild();
//MbElement English = Student.getNextSibling();
//MbElement Maths = Student.getLastChild();
//MbElement Total = Student.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Total", "55");
//MbElement Total = Student.createElementAsLastChild(MbXML.ASIS_ELEMENT_CONTENT,"Total","94");
// add title attribute
//MbElement Total = Student.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Total", "88");
out.propagate(outAssembly);
}
finally
{
outMessage.clearMessage();
}
}
Regards
Sithanandam.V |
|
Back to top |
|
 |
rekarm01 |
Posted: Fri Jul 29, 2011 4:48 am Post subject: Re: How to Create New XML Element Using JCompute Node? |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
The XML parser is deprecated. XMLNSC is a better choice.
anandsitha wrote: |
I have tried with
MbElement Total = Student.createElementAsFirstChild(MbElement.TYPE_NAME_VALUE, "Total", "88") but it is not working. |
The XML parser requires two elements, TYPE_NAME (XML.Element), and TYPE_VALUE (XML.Content).
The XMLNSC parser requires one element, TYPE_NAME_VALUE (XMLNSC.Field).
Consider using the parser-specific field type constants, rather than the generic constants. |
|
Back to top |
|
 |
lancelotlinc |
Posted: Fri Jul 29, 2011 4:55 am Post subject: |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
|
Back to top |
|
 |
MQWMBDeveloper |
Posted: Fri Jul 29, 2011 10:45 am Post subject: Creating xml element using Javacompute node. |
|
|
Novice
Joined: 28 Jul 2011 Posts: 16
|
I just did the same for my project, here's the code.
//create the parse element first
MbElement outParser = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
//create the xml element.
MbElement Total= outParser .createElementAsFirstChild(MbXMLNSC.FIELD,"Total",8 ; |
|
Back to top |
|
 |
MQWMBDeveloper |
Posted: Fri Jul 29, 2011 10:47 am Post subject: Creating xml element using Javacompute node. |
|
|
Novice
Joined: 28 Jul 2011 Posts: 16
|
I meant to say "88"); at the end and close brackets |
|
Back to top |
|
 |
lancelotlinc |
Posted: Fri Jul 29, 2011 10:54 am Post subject: Re: Creating xml element using Javacompute node. |
|
|
 Jedi Knight
Joined: 22 Mar 2010 Posts: 4941 Location: Bloomington, IL USA
|
MQWMBDeveloper wrote: |
I meant to say "88"); at the end and close brackets |
If you had used [ c o d e ] tag. you would have gotten it right. Did you actually read the post? _________________ http://leanpub.com/IIB_Tips_and_Tricks
Save $20: Coupon Code: MQSERIES_READER |
|
Back to top |
|
 |
anandsitha |
Posted: Mon Aug 01, 2011 12:57 am Post subject: |
|
|
Acolyte
Joined: 26 Jul 2011 Posts: 59
|
Hi-
//create the parse element first
MbElement outParser = outMessage.getRootElement().createElementAsLastChild(MbXMLNSC.PARSER_NAME);
//create the xml element.
MbElement Total= outParser .createElementAsFirstChild(MbXMLNSC.FIELD,"Total",8 ;
This code only create the output file with <Total>88</Total>. my input file format is below
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<Rollno>22222</Rollno>
<Name>Sithanandam</Name>
<English>95</English>
<Maths>40</Maths>
</Student>
Total Should be calculated ie. Total = English + Maths
Till now in my code
public void evaluate(MbMessageAssembly assembly) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage inMessage = assembly.getMessage();
MbMessage outMessage = new MbMessage(inMessage);
MbMessageAssembly outAssembly = new MbMessageAssembly(assembly,
outMessage);
try
{
MbElement root = assembly.getMessage().getRootElement();
MbElement Student = root.getLastChild().getFirstChild();
MbElement RollNo = Student.getFirstChild();
MbElement English = Student.getNextSibling();
MbElement Maths = Student.getLastChild();
out.propagate(outAssembly);
}
finally
{
outMessage.clearMessage();
}
}
How can we modify the above code to calculate Total. And Generate the below output file.
<?xml version="1.0" encoding="UTF-8"?>
<Student>
<Rollno>22222</Rollno>
<Name>Sithanandam</Name>
<English>95</English>
<Maths>40</Maths>
<Total>135</Total>
</Student>
Regards
Sithanandam.V |
|
Back to top |
|
 |
rekarm01 |
Posted: Mon Aug 01, 2011 1:54 am Post subject: Re: Creating xml element using Javacompute node. |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
Please put [code] tags around any posted code, to make it more readable.
Use the [Edit] button in the upper right corner of existing posts to fix them, and use the [Preview] button before submitting new posts, to make sure that they are readable.
If outMessage already has an XMLNSC element as last child, it doesn't need another one. |
|
Back to top |
|
 |
|