Author |
Message
|
WMBSAM |
Posted: Wed Feb 24, 2010 4:06 am Post subject: extract xml from the mbelement |
|
|
 Voyager
Joined: 02 Oct 2009 Posts: 90 Location: Atlanta
|
hi ther,
I am trying to extract the xml from the mbelement but i fails to do so.
is there any way that we can extract the xml from the mbelement?
here is my code:
Code: |
public void evaluate(MbMessageAssembly contact admin) throws MbException {
MbOutputTerminal out = getOutputTerminal("out");
MbOutputTerminal alt = getOutputTerminal("alternate");
MbMessage inMessage = contact admin.getMessage();
// create new message
MbMessage outMessage = new MbMessage();
MbMessageAssembly outAssembly = new MbMessageAssembly(contact admin,
outMessage);
try {
// optionally copy message headers
copyMessageHeaders(inMessage, outMessage);
// ----------------------------------------------------------
// Add user code below
try {
MbElement xml = inMessage.getRootElement().getLastChild().getFirstChild();
MbElement outRoot = outMessage.getRootElement();
MbElement outBody = outRoot.createElementAsLastChild("XMLNSC");
outBody.addAsLastChild(xml.copy());
MbMessage msg = outAssembly.getMessage();
// converting the msg to bitstream
byte[] bitstream = msg.getBuffer();
String myfile = "C:/sample.txt";
// converting bitstream back to string
String b = bitstream.toString();
FileOutputStream outputStream = new FileOutputStream(myfile);
// trying to print bitstream
outputStream.write(bitstream);
outputStream.close();
PrintWriter pw = new PrintWriter(new FileWriter("C:/sample.txt"));
// trying to print string assigned to variable b
pw.print(b);
pw.print(' ');
pw.println();
pw.close();
}
catch (Throwable e) {
System.err.println(e);
}
// End of user code
// ----------------------------------------------------------
// The following should only be changed
// if not propagating message to the 'out' terminal
out.propagate(outAssembly);
} finally {
// clear the outMessage even if there's an exception
outMessage.clearMessage();
}
}
|
in the above code i am trying to convert the mbelement to bitstream first and then string so that i can use it to pass to other java application but the conversion to string from the bitstream fails.
to be clear on the whole i am trying to extract either xml or the string(full inputbody) from the input message. so that i can pass it to other method defined in other java program but it fails.
note: i am able to print the bitstream perrfectly to a file but when i convert it to a string(bitstream) it doesn't.
 |
|
Back to top |
|
 |
kimbert |
Posted: Wed Feb 24, 2010 4:41 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
but the conversion to string from the bitstream fails. |
How does it 'fail'? |
|
Back to top |
|
 |
mqjeff |
Posted: Wed Feb 24, 2010 5:26 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Of course getBuffer() is the wrong method to use in the first place. |
|
Back to top |
|
 |
Bartez75 |
Posted: Wed Feb 24, 2010 7:02 am Post subject: |
|
|
 Voyager
Joined: 26 Oct 2006 Posts: 80 Location: Poland, Wroclaw
|
Try to use something like
Code: |
byte [] msgAsBytes = inMessage.getRootElement().getLastChild().toBitstream(null,null,null,0,ccsid,0);
String msgAsText = new String(msgAsBytes); |
where ccsid is the proper ccsid code. |
|
Back to top |
|
 |
WMBSAM |
Posted: Thu Feb 25, 2010 6:40 pm Post subject: |
|
|
 Voyager
Joined: 02 Oct 2009 Posts: 90 Location: Atlanta
|
thanks bartez your suggestion worked!!
now i have another issue how can i convert a string into a mbelement i mean how to i put the sting into a mbelement.
DETAils:
I call a java method with the above converted string and it then sends the response back which is again in the string format now i need to put this string(which is a xml in string format ex:"<xml></xml>") back into the Mbelement to propagate to output terminal.Any ideas???
 |
|
Back to top |
|
 |
fjb_saper |
Posted: Thu Feb 25, 2010 9:08 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
WMBSAM wrote: |
thanks bartez your suggestion worked!!
now i have another issue how can i convert a string into a mbelement i mean how to i put the sting into a mbelement.
DETAils:
I call a java method with the above converted string and it then sends the response back which is again in the string format now i need to put this string(which is a xml in string format ex:"<xml></xml>") back into the Mbelement to propagate to output terminal.Any ideas???
 |
Why do you even bother to serialize into an xml string that you would have to parse again? Are you changing parser and deem this faster than just copying the tree, or less complicated (xmlns to xmlnsc)? What does the manual say? Have you checked what it says about copying the tree? Have you checked the examples? Have you looked at how they copied the headers?
Help yourself some and the manuals, its all there
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
WMBSAM |
Posted: Fri Feb 26, 2010 8:49 am Post subject: |
|
|
 Voyager
Joined: 02 Oct 2009 Posts: 90 Location: Atlanta
|
Quote: |
Why do you even bother to serialize into an xml string that you would have to parse again? Are you changing parser and deem this faster than just copying the tree, or less complicated (xmlns to xmlnsc)? What does the manual say? Have you checked what it says about copying the tree? Have you checked the examples? Have you looked at how they copied the headers? |
hello fjb_saper as i said before the need to serialize the mbelement to string is becoz there is a functionality written in java program where it accepts the argument in the form of xml string and then returns a xml string which i need to convert back to mbelement to send to output terminal.. |
|
Back to top |
|
 |
fjb_saper |
Posted: Fri Feb 26, 2010 12:56 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
WMBSAM wrote: |
Quote: |
Why do you even bother to serialize into an xml string that you would have to parse again? Are you changing parser and deem this faster than just copying the tree, or less complicated (xmlns to xmlnsc)? What does the manual say? Have you checked what it says about copying the tree? Have you checked the examples? Have you looked at how they copied the headers? |
hello fjb_saper as i said before the need to serialize the mbelement to string is becoz there is a functionality written in java program where it accepts the argument in the form of xml string and then returns a xml string which i need to convert back to mbelement to send to output terminal.. |
I believe both the serialization and the parsing of such a string have been talked about on the forum and are documented in the infocenter.
The search button is your friend.
 _________________ MQ & Broker admin |
|
Back to top |
|
 |
WMBSAM |
Posted: Fri Feb 26, 2010 5:00 pm Post subject: |
|
|
 Voyager
Joined: 02 Oct 2009 Posts: 90 Location: Atlanta
|
hello fjb_saper wherever you are God bless you, your advice worked . I might have done some research and approached over here. yes the infocenter doc and some research solved my issue . Thanks alot!!!  |
|
Back to top |
|
 |
|