• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to convert a StreamResult to a String?

 
Daniel Martins
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better suggestion by Bernhard below, so I deleted this!
 
Bernhard Haeussermann
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tks a lot, I could solve it
 
Matt CampbellZ
Greenhorn
Posts: 1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic