aspose file tools*
The moose likes Web Services and the fly likes Getting SOAP response as XML string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Getting SOAP response as XML string" Watch "Getting SOAP response as XML string" New topic
Author

Getting SOAP response as XML string

Y Enev
Greenhorn

Joined: Oct 14, 2006
Posts: 8
Hello,
I have a web service that returns an object. I also have an AXIS generated client that calls this web service and gets the response. In my client I need to get an XML representation of the response object as a string, the soap body will suffice. So far I am unable to find a way of doing this. I see that in the generated by AXIS class that represents the response there's a method called getSerializer, but I don't know how to use it in order to get the xml,if it's possible at all.
Can anybody help?
Thanks.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
For Axis 1.x:
Write a class that implements the javax.xml.rpc.handler.Handler interface (or inherits from javax.xml.rpc.handler.GenericHandler) and register this message handler with the client stub.

During handleResponse() you are handed a javax.xml.rpc.handler.MessageContext on which you can call getMessage() - that will hand you a javax.xml.soap.SOAPMessage.

SOAPMessage's writeTo() can be used to write the message to a ByteArrayOutputStream on which you can finally call toString().

If you are only interested in the SOAPMessage and not the Axis object representation you may want to consider a pure SAAJ client instead.

Java Web Services Programming: SAAJ
Y Enev
Greenhorn

Joined: Oct 14, 2006
Posts: 8
Thanks.
This is a possible approach, but in my case I need the XML within the client, not before the response reaches the client. The idea behind this is that I have to populate the data from the response in a jsp. I can, of course, write the JSP and reference every field from the response object, but I would like to do it using XML/XSLT transformation.That's why I would like to have the XML representation of the object.
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Y Enev:
but in my case I need the XML within the client, not before the response reaches the client.

Handlers can be installed on the client side, not just on the server side. You would however have to provide a place where the handler can place the SOAP message so that the client code can access it later.

In any case sounds like the you should give the SAAJ client a closer, serious look.

If you use the ByteArrayOutputStream to create a ByteArrayInputStream you can create a StreamSource as an input to your transformation.
[ September 06, 2007: Message edited by: Peer Reynders ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting SOAP response as XML string