how to send XML Message using JMS

Hello friends,

I need to know , how do we send XML Messages usins JMS message service.

I found that we can send it using TextMessage but for this i will have to manually make a string out of my XML some what in this fashion.

StringBuffer buffer =new StringBuffer();

buffer.append("<?xml version='1.0'?>\n");

buffer.append("<UserProfiles>\n");

buffer.append("<User>\n");

buffer.append("<Action>Create</Action>\n");

buffer.append("<Username>"+username+"</Username>\n");

buffer.append("<FirstName>"+firstName+"</FirstName>\n");

buffer.append("<LastName>"+lastName+"</FirstName>\n");

buffer.append("<Email>"+email+"</Email>\n");

buffer.append("</User>\n");

buffer.append("</UserProfiles>");

But i dont want to do these. I will create XML using DOM , so that means my entire XML would be inside Document object, So is there any way of converting this Document object to String so that i can send it using TextMessage. thanks.

[1498 byte] By [son_of_a_gunna] at [2007-10-2 6:09:58]
# 1

You can try the following code-snippet for your problem

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(node);

// node is the root-node of the DOM object.

StringWriter buffer = new StringWriter();

StreamResult result = new StreamResult(buffer);

transformer.transform(source, result);

String xmlString = buffer.toString();

Now you can use this "xmlString" to create a TextMessage for JMS

Hope this will solve your problem...

ArijitSarkara at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

> You can try the following code-snippet for your

> problem

>

> TransformerFactory tFactory =

> TransformerFactory.newInstance();

> Transformer transformer = tFactory.newTransformer();

>

> DOMSource source = new DOMSource(node);

> // node is the root-node of the DOM object.

>

> StringWriter buffer = new StringWriter();

>

> StreamResult result = new StreamResult(buffer);

> transformer.transform(source, result);

>

> String xmlString = buffer.toString();

>

> Now you can use this "xmlString" to create a

> TextMessage for JMS

> Hope this will solve your problem...

Hey man , tryin this out. if it works u will get duke dollars from me ;-)

son_of_a_gunna at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
hey man , its workin , have gifted you one duke dollar as a gesture for your timely help :-) , coz thats all i could do .
son_of_a_gunna at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
It's my pleasure to help you. Anyway thanks for the duke-dollar.
ArijitSarkara at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

> It's my pleasure to help you. Anyway thanks for the

> duke-dollar.

Hey Buddy,thats really nice of you.

Can u kindly tell me the reverse process of it too. i mean once i send that message , on the Receicer side i have to convert this string to corresponding XML Document Object or Say a Document Node.

i might be giving an impression of a self-seeker , but i have been strugglin on this since mornin :-( , and had tried every d_a_m_n thing from JAXB to Java.beans.XMLEncoder etc till you came up with such a nice solution.

Thanks once again.

Cheers.

son_of_a_gunna at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

sorry buddy for late reply. actually there was some problem while logging on.

here's your answer. try this, this should solve your problem.

Document dom = null;

try {

byte[] buffer = xmlString.getBytes();

StreamSource source = new StreamSource(new ByteArrayInputStream(buffer));

dom = getEmptyXMLDocument();

DOMResult result = new DOMResult(dom);

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

transformer.transform(source, result);

} catch (TransformerConfigurationException e) {

e.printStackTrace();

} catch (TransformerException e) {

e.printStackTrace();

}

try this and let me know...

ArijitSarkara at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
NB: "xmlString" is String converted from DOM.
ArijitSarkara at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8

> sorry buddy for late reply. actually there was some

> problem while logging on.

>

> here's your answer. try this, this should solve your

> problem.

>

> Document dom = null;

>

> try {

>byte[] buffer = xmlString.getBytes();

> StreamSource source = new StreamSource(new

> e(new ByteArrayInputStream(buffer));

>dom = getEmptyXMLDocument();

>

>DOMResult result = new DOMResult(dom);

> TransformerFactory tFactory =

> ory = TransformerFactory.newInstance();

> Transformer transformer =

> mer = tFactory.newTransformer();

>transformer.transform(source, result);

>

>} catch (TransformerConfigurationException e) {

> e.printStackTrace();

>} catch (TransformerException e) {

> e.printStackTrace();

>}

>

> try this and let me know...

Hey dude, thanks once again for bothering. i did it this way

DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = db.parse(new InputSource(new StringReader(sapString)));

Cheers.

son_of_a_gunna at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9
cheers!
ArijitSarkara at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 10
> cheers!Hey arijit, have you worked on JMS , especially the request-response stuff , if yes please do let me know as i have to implement it and am not clear on how do i go about it
son_of_a_gunna at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 11

I have used JMS in the following way...

Lets say, i have a message pool containing some tasks and a java program sending messages to a queue (server-side where task is getting executed). "MessageSender" sends a message to the queue and "Message-Reciever" recieves it.

When "Message-Reciever" finishes its task, replies a notification-message to the message-pool managing program, so that it can submit next task.

For my implementation it's required that only 1 task will be executed at a time, but many tasks can be submitted simultaneously by clients.

For this implementation, i have used the following properties in the JMS-Message:

User-defined Message-Properties: like, "Task-type" (type of submitted task), "Message-Type"(for checking the type of message, whether a task-submission or task-ending notification) etc.

That's what i can help you right now. 'Coz i am still learning JMS and how to use it efficiently.

take care...

ArijitSarkara at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 12
hmmm.... ok.thats quite different from mine. i have to implement an asynchronous request-response approach.neways thanks
son_of_a_gunna at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 13

Hello Everyone,

I am a newbie in this forum. I would just like to ask about JMS and XML? How is it possible for JMS to send XML? I am a beginner in using JMS, for now I am learning the Point-to-Point and Publisher-Subscriber Communication. Can I send XML using these methods? If it's ok, I would like to have a sample code for this module. Also, I would like to know what really is DOM (Document Object Model) and what is its relation to XML.Thank you very much..

James

jamlsa at 2007-7-16 13:10:39 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...