【问题标题】:Simpler way to transform a DOMSource into a StreamSource?将 DOMSource 转换为 StreamSource 的更简单方法?
【发布时间】:2012-04-15 21:53:15
【问题描述】:

我需要将DOMSource 转换为StreamSource,因为第三方库只接受SOAP 的流源。

在这种情况下,性能并不是什么大问题,所以我想出了这组极其冗长的命令:

DOMSource src = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
StreamResult result = new StreamResult();
ByteArrayOutputStream out = new ByteArrayOutputStream();
result.setOutputStream(out);
transformer.transform(src, result);
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
StreamSource streamSource = new StreamSource(in);

没有更简单的方法吗?

【问题讨论】:

    标签: java xml dom transform


    【解决方案1】:

    这是一种很好的方法。因为您的第三方库只接受词法形式的 XML,所以您别无选择,只能序列化 DOM,以便外部库可以重新解析它。愚蠢的设计 - 告诉他们。

    【讨论】:

    • “他们”?你的意思是Apache和Weblogic? :-) 有趣的是,方法签名接受任何Source,但反编译的实现悄悄地拒绝除StreamSource之外的所有内容...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 2020-05-17
    • 2011-09-18
    • 1970-01-01
    • 2020-10-03
    • 2012-09-02
    • 2011-10-08
    相关资源
    最近更新 更多