gpt4 book ai didi

java - 将 StreamResult 转换为字符串或 xml

转载 作者:IT老高 更新时间:2023-10-28 20:21:07 30 4
gpt4 key购买 nike

使用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;

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

我该怎么做?

最佳答案

试试这个:

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

关于java - 将 StreamResult 转换为字符串或 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23219728/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com