Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert StreamResult to string or xml

Using spring ws to get the StreamResult as below

StreamSource source = new StreamSource(new StringReader(MESSAGE)); StreamResult result = new StreamResult(System.out); webServiceTemplate.sendSourceAndReceiveToResult("http://someUri",                  source, new SoapActionCallback("someCallBack"), result);  return result; 

I get the result, But I want to extract it to some sort of xml or even as a string (Just want to see the contents in order to generate the response).

How can I do this?

like image 990
user1609085 Avatar asked Apr 22 '14 12:04

user1609085


People also ask

How to convert StreamResult to String in java?

StreamSource source = new StreamSource(new StringReader(MESSAGE)); StreamResult result = new StreamResult(System. out); webServiceTemplate.

What is StreamResult in Java?

public class StreamResult extends Object implements Result. Acts as an holder for a transformation result, which may be XML, plain Text, HTML, or some other form of markup.

What is DOMSource in Java?

public class DOMSource extends Object implements Source. Acts as a holder for a transformation Source tree in the form of a Document Object Model (DOM) tree. Note that XSLT requires namespace support. Attempting to transform a DOM that was not contructed with a namespace-aware parser may result in errors.


1 Answers

Try this one:

try {     StreamSource source = new StreamSource(new StringReader("<xml>blabla</xml>"));     StringWriter writer = new StringWriter();     StreamResult result = new StreamResult(writer);     TransformerFactory tFactory = TransformerFactory.newInstance();     Transformer transformer = tFactory.newTransformer();     transformer.transform(source,result);     String strResult = writer.toString(); } catch (Exception e) {     e.printStackTrace(); } 
like image 196
Asif Bhutto Avatar answered Sep 18 '22 15:09

Asif Bhutto