将StreamResult转换为字符串或xml [英] Convert StreamResult to string or xml

查看:1123
本文介绍了将StreamResult转换为字符串或xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用spring ws获取StreamResult,如下所示

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;

我得到了结果,但我想把它提取到某种xml甚至是字符串(只是想查看内容以生成响应)。

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).

我该怎么做?

推荐答案

试试这个:

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();
}

这篇关于将StreamResult转换为字符串或xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆