Java 将 StreamResult 转换为字符串或 xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23219728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert StreamResult to string or xml
提问by user1609085
Using spring ws to get the StreamResult as below
使用 spring ws 获取 StreamResult 如下
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).
我得到了结果,但我想将它提取为某种 xml 甚至作为字符串(只想查看内容以生成响应)。
How can I do this?
我怎样才能做到这一点?
采纳答案by Asif Bhutto
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();
}
回答by Lex Webb
You can get the reader of your StreamSource by using getReader(). You should then be able to use read(char[] cbuf) to write the contents of the stream to a character array which can easily be converted into a string and printed to the console if you wish.
您可以使用 getReader() 获取 StreamSource 的阅读器。然后,您应该能够使用 read(char[] cbuf) 将流的内容写入字符数组,如果您愿意,可以轻松地将其转换为字符串并打印到控制台。
回答by murasing
If none of these works, try this
如果这些都不起作用,试试这个
System.out.println(result.getOutputStream().toString());
Assuming you have this kind of structure ,
假设你有这种结构,
private static StreamResult printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
return result;
}
You can try this way , although the same thing, wanted to point it out clearly
你可以试试这个方法,虽然是同一个东西,想指出清楚
System.out.println(printSOAPResponse(soapResponse).getOutputStream().toString());
回答by the hand of NOD
If you use Spring you could also use this way:
如果您使用 Spring,您也可以使用这种方式:
import org.springframework.core.io.Resource;
import org.apache.commons.io.IOUtils;
....
@Value("classpath:/files/dummyresponse.xml")
private Resource dummyResponseFile;
....
public String getDummyResponse() {
try {
if (this.dummyResponse == null)
dummyResponse = IOUtils.toString(dummyResponseFile.getInputStream(),StandardCharsets.UTF_8);
} catch (IOException e) {
logger.error("Fehler in Test-Service: {}, {}, {}", e.getMessage(), e.getCause(), e.getStackTrace());
throw new RuntimeException(e);
}
return dummyResponse;
}