Forums Register Login

How to convert a StreamResult to a String?

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Better suggestion by Bernhard below, so I deleted this!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Tks a lot, I could solve it:)
1
+Pie Number of slices to send: Send
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();
+Pie Number of slices to send: Send
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
Good heavens! What have you done! Here, try to fix it with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 24182 times.
Similar Threads
Webservice not getting the parameter, parameter is null
problem while calling document style web service
Output as null
Print out the SOAP request
Need to send Signed SOAP Message
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:04:31.