aspose file tools*
The moose likes Web Services and the fly likes How to convert a StreamResult to a String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to convert a StreamResult to a String?" Watch "How to convert a StreamResult to a String?" New topic
Author

How to convert a StreamResult to a String?

Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Hi

I have this code, that retreives me an XML message. How can I convert the result (Type ResultStream) to a String type?

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();

// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();

// Object for message parts
SOAPPart sp = msg.getSOAPPart();
StreamSource prepMsg = new StreamSource(
new FileInputStream("D:/SoapRequest/soap.txt"));
sp.setContent(prepMsg);

// Save message
msg.saveChanges();

// View input
System.out.println("\n Soap request:\n");

msg.writeTo(System.out);
System.out.println();

// Send
Authenticator.setDefault(new MyAuthenticator());
String urlval = "http://....";
SOAPMessage rp = conn.call(msg, urlval);

// View the output
System.out.println("\nXML response\n");

// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

// Get reply content
Source sc = rp.getSOAPPart().getContent();

// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();


This works, and I get the soap response printed...however i need a String to save it to a file.

Thank you in advance
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Better suggestion by Bernhard below, so I deleted this!
Bernhard Haeussermann
Ranch Hand

Joined: Aug 05, 2009
Posts: 38
Have a look at and learn to use the javadocs of the StreamResult class (http://java.sun.com/javase/6/docs/api/).
One of the constructors of StreamResult takes a Writer object as a parameter. You will see that one of the sub-classes of Writer is StringWriter. So to obtain a string from what is written to the StreamResult, you can construct a StringWriter, put it into the StreamResult, transform() the Source to the StreamResult and get the string from the StringWriter.
You can also write the Source to a file directly by passing a FileOutputStream to the constructor of StreamResult. Actually, there is an even easier way since StreamResult also has a constructor that takes a File object as a parameter.
Hope this helps.
Daniel Martins
Ranch Hand

Joined: Oct 22, 2009
Posts: 45
Tks a lot, I could solve it
Matt CampbellZ
Greenhorn

Joined: Jan 28, 2011
Posts: 1
Just to mention it for the real newbies, you need to use the StringBuffer from the StringWriter to get to the text held in the writer. So the code looks like this:

StringWriter sw = (StringWriter) streamresult.getWriter(); // Assume that the StreamResult object was created with a StringWriter object,
// otherwise you'll get a class cast exception here
StringBuffer sb = sw.getBuffer();
String finalstring = sb.toString();
Hasham Shaik
Greenhorn

Joined: Jun 27, 2012
Posts: 1
Hi, I'm new to Java, could somebody mention the steps to get the source in last lines of the below code that is printed on console be assigned to a String variable?

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
SOAPConnection conn = scf.createConnection();

// Create message
MessageFactory mf = MessageFactory.newInstance();
SOAPMessage msg = mf.createMessage();

// Object for message parts
SOAPPart sp = msg.getSOAPPart();
StreamSource prepMsg = new StreamSource(
new FileInputStream("D:/SoapRequest/soap.txt"));
sp.setContent(prepMsg);

// Save message
msg.saveChanges();

// View input
System.out.println("\n Soap request:\n");

msg.writeTo(System.out);
System.out.println();

// Send
Authenticator.setDefault(new MyAuthenticator());
String urlval = "http://....";
SOAPMessage rp = conn.call(msg, urlval);

// View the output
System.out.println("\nXML response\n");

// Create transformer
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer();

// Get reply content
Source sc = rp.getSOAPPart().getContent();

// Set output transformation
StreamResult result = new StreamResult(System.out);
tf.transform(sc, result);
System.out.println();

Thanks in Advance
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert a StreamResult to a String?
 
Similar Threads
problem while calling document style web service
Webservice not getting the parameter, parameter is null
Output as null
Need to send Signed SOAP Message
Print out the SOAP request