Author |
Message
|
satya2481 |
Posted: Thu Dec 09, 2010 8:53 pm Post subject: How to insert an MbElement into another MbElement |
|
|
Disciple
Joined: 26 Apr 2007 Posts: 170 Location: Bengaluru
|
Hi All,
I am using WMB V6.1 Toolkit and Server... I am trying to copy one MbElement into another MbElement and not succeeding. If anyone knows how to do it please advice...
My requirement is as below.
1. Read input message as BLOB data
2. Parse the message against XMLNS domain and get it in tree format
3. Get a particular field and its childrens from RFH2.other folder
4. Insert the data retrieved from RFH2 header in step3 into the parsed message obtained in step2
I am trying to code as below.
Code: |
// Get BLOB Message
MbElement inBLOBMessage = inRoot.getFirstElementByPath("/BLOB");
byte[] actualBLOB = inBLOBMessage.toBitstream(null, null, null, inEncoding, inCCSID, 0);
// Parse the BLOB data against XMLNS parser
MbElement inParsedMessage = outRoot.createElementAsLastChildFromBitstream(actualBLOB, MbXMLNS.PARSER_NAME, "", "", "", inEncoding, inCCSID, 0);
// Get the reference to the Parsed message where RFH2 header data needs to be inserted
MbElement inPlaceToInsertData = inParsedMessage.getFirstChild().getNextSibling().getFirstChild().getNextSibling();
// Get the elements which needs to be inserted in the data from RFH2 header
MbElement inData1 = inRoot.getFirstElementByPath("/MQRFH2C/Data/Test/RequiredData1");
MbElement inData2 = inRoot.getFirstElementByPath("/MQRFH2C/Data/Test/RequiredData2");
MbElement outData = inHeader.createElementAsLastChild(MbElement.TYPE_NAME, "HeaderData", "");
outData.copyElementTree(inData1);
// When below statement gets executed the first copied data getting lost that is inData1 lost and inData2 takes place
outData.copyElementTree(inData2); |
I have tried below methods as well.
Code: |
outData.addAsLastChild(inData1); |
This statement throws an exception...
After the checking the details of addAsLastChild method it says "Adds an unattached syntax element as the last of this element." In this statement whats "unattached syntax element" means ?
Regards
Satya |
|
Back to top |
|
 |
j.f.sorge |
Posted: Fri Dec 10, 2010 12:02 am Post subject: Re: How to insert an MbElement into another MbElement |
|
|
Master
Joined: 27 Feb 2008 Posts: 218
|
satya2481 wrote: |
...
Code: |
outData.addAsLastChild(inData1); |
This statement throws an exception...
... |
It would be nice to know which Exception has been thrown. _________________ IBM Certified Solution Designer - WebSphere MQ V6.0
IBM Certified Solution Developer - WebSphere Message Broker V6.0
IBM Certified Solution Developer - WebSphere Message Broker V6.1
IBM Certified Solution Developer - WebSphere Message Broker V7.0 |
|
Back to top |
|
 |
satya2481 |
Posted: Fri Dec 10, 2010 1:08 am Post subject: |
|
|
Disciple
Joined: 26 Apr 2007 Posts: 170 Location: Bengaluru
|
Hi sorge,
I have checked on below statement
Code: |
outData.addAsLastChild(inData1); |
It should be written as below
Code: |
outData.addAsLastChild(inData1.copy()); |
This will not give any Exception. This statement copying the elements but all the child elements of the Root element copied getting created as Attributes
I am trying to fix this issue...
Do we need to write a recursive function? So that element by element copy can be done !!! |
|
Back to top |
|
 |
satya2481 |
Posted: Fri Dec 10, 2010 1:48 am Post subject: |
|
|
Disciple
Joined: 26 Apr 2007 Posts: 170 Location: Bengaluru
|
Just to provide an update on the copyElementTree Method...
Quote: |
copyElementTree
public void copyElementTree(MbElement element) throws MbException
Copies the element tree from the element passed as an argument to the current element. Only the child elements of the source element are copied. Before the copy is performed, all existing child elements of the target (this) element are deleted, to be replaced by the child elements of the source element.
Parameters
element - Source element for copy.
Throws
MbException |
From IBM Help - http://publib.boulder.ibm.com/infocenter/wmbhelp/v6r0m0/index.jsp?topic=/com.ibm.etools.mft.doc/com/ibm/broker/plugin/MbElement.html |
|
Back to top |
|
 |
j.f.sorge |
Posted: Fri Dec 10, 2010 2:04 am Post subject: |
|
|
Master
Joined: 27 Feb 2008 Posts: 218
|
It seems to be that you are trying to change the input message (but I'm not sure). You should copy the message headers within the JCN and create a new output message body with XMLNSC parser domain. This domain should contain a root element with the original data and the data you want to add. _________________ IBM Certified Solution Designer - WebSphere MQ V6.0
IBM Certified Solution Developer - WebSphere Message Broker V6.0
IBM Certified Solution Developer - WebSphere Message Broker V6.1
IBM Certified Solution Developer - WebSphere Message Broker V7.0 |
|
Back to top |
|
 |
satya2481 |
Posted: Fri Dec 10, 2010 2:17 am Post subject: |
|
|
Disciple
Joined: 26 Apr 2007 Posts: 170 Location: Bengaluru
|
No,
I am not trying to change input message.
In the code I have given outRoot is nothing but...
MbMessage outMsg = new MbMessage();
MbElement outRoot = outMsg.getRootElement();
So I am creating a new output message which is having below values...
OutputRoot.Properties = InputRoot.Properties
OutputRoot.MQMD = InputRoot.Properties
OutputRoot.XMLNS = (InputRoot.BLOB parsed to XMLNS Domain + Some of the data extracted from RFH2 header)
Example requirement...
Parsed input BLOB Message
Code: |
<MsgRoot>
<Name>
<FName>TestFirstName</FName>
<LName>TestLastName</LName>
</Name>
</MsgRoot> |
Incoming message RFH2.other folder content
Code: |
<Data>
<RequiredData1 value="123">
<NewName>
<FName>ABC</FName>
<LName>DEF</LName>
</NewName>
</RequiredData1>
<SomeOtherData>
<Child>XYZ</Child>
</SomeOtherData>
<RequiredData2 value="123">
<NewName>
<Name1>ABC1</Name1>
<Name2>DEF2</Name2>
</NewName>
</RequiredData2>
</Data> |
My Output Message should look like as below....
Code: |
<MsgRoot>
<Name>
<FName>TestFirstName</FName>
<LName>TestLastName</LName>
</Name>
<NewData>
<RequiredData1 value="123">
<NewName>
<FName>ABC</FName>
<LName>DEF</LName>
</NewName>
</RequiredData1>
<RequiredData2 value="123">
<NewName>
<Name1>ABC1</Name1>
<Name2>DEF2</Name2>
</NewName>
</RequiredData2>
</NewData>
</MsgRoot> |
I hope the requirement is clear with this sample... |
|
Back to top |
|
 |
mqjeff |
Posted: Fri Dec 10, 2010 3:01 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
You could detach() the element, and then it would probably work to addAfter().
But I think you would not end up with a valid output tree, as the elements under MQRFH2 header are not going to be serializable with the XMNLS or XMLNSC parser.
And you should be using XMLNSC anyway.
So I think it's best if you write a routine to do a tree copy. |
|
Back to top |
|
 |
satya2481 |
Posted: Sun Dec 12, 2010 9:39 pm Post subject: |
|
|
Disciple
Joined: 26 Apr 2007 Posts: 170 Location: Bengaluru
|
Hi All,
The problem got resolved...
I was trying to update existing message flow. Where in the MQRFH2C compact parser check box was selected in the flow Input Node. This was causing elements to be created as Attributes...
I have unchecked the option and now all the elements are getting created correctly when I copy the RFH2 details into Incoming message.
- Satya |
|
Back to top |
|
 |
|