Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Raw XML From SOAPMessage in Java

Tags:

java

soap

jax-ws

I've set up a SOAP WebServiceProvider in JAX-WS, but I'm having trouble figuring out how to get the raw XML from a SOAPMessage (or any Node) object. Here's a sample of the code I've got right now, and where I'm trying to grab the XML:

@WebServiceProvider(wsdlLocation="SoapService.wsdl") @ServiceMode(value=Service.Mode.MESSAGE) public class SoapProvider implements Provider<SOAPMessage> {     public SOAPMessage invoke(SOAPMessage msg)     {         // How do I get the raw XML here?     } } 

Is there a simple way to get the XML of the original request? If there's a way to get the raw XML by setting up a different type of Provider (such as Source), I'd be willing to do that, too.

like image 544
Dan Lew Avatar asked Feb 06 '09 22:02

Dan Lew


2 Answers

You could try in this way.

SOAPMessage msg = messageContext.getMessage(); ByteArrayOutputStream out = new ByteArrayOutputStream(); msg.writeTo(out); String strMsg = new String(out.toByteArray()); 
like image 160
Smith Torsahakul Avatar answered Oct 08 '22 21:10

Smith Torsahakul


If you have a SOAPMessage or SOAPMessageContext, you can use a Transformer, by converting it to a Source via DOMSource:

            final SOAPMessage message = messageContext.getMessage();             final StringWriter sw = new StringWriter();              try {                 TransformerFactory.newInstance().newTransformer().transform(                     new DOMSource(message.getSOAPPart()),                     new StreamResult(sw));             } catch (TransformerException e) {                 throw new RuntimeException(e);             }              // Now you have the XML as a String:             System.out.println(sw.toString()); 

This will take the encoding into account, so your "special characters" won't get mangled.

like image 35
artbristol Avatar answered Oct 08 '22 22:10

artbristol