java - 文档到 StreamSource 的转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4722482/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 07:42:50  来源:igfitidea点击:

java - Document to StreamSource conversion

javadomxslt

提问by tommyk

For XSLT transformation I need a javax.xml.transform.stream.StreamSourceobject representing xml file being transformed. I have only an object of org.w3c.dom.Documenttype. How to convert Document to a StreamSource ?

对于 XSLT 转换,我需要一个表示被转换的 xml 文件的javax.xml.transform.stream.StreamSource对象。我只有一个org.w3c.dom.Document类型的对象。如何将 Document 转换为 StreamSource ?

回答by tommyk

I found a solution on this web page. There is a DOMSourceclass which takes Document object as constructor parameter.

我在此网页上找到了解决方案。有一个DOMSource类,它将 Document 对象作为构造函数参数。

/**
 * Convert document to string for display
 * @param doc org.w3c.dom.Document
 * @return String
 */
private String documentToString(org.w3c.dom.Document doc) throws TransformerException {

    // Create dom source for the document
    DOMSource domSource=new DOMSource(doc);

    // Create a string writer
    StringWriter stringWriter=new StringWriter();

    // Create the result stream for the transform
    StreamResult result = new StreamResult(stringWriter);

    // Create a Transformer to serialize the document
    TransformerFactory tFactory =TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty("indent","yes");

    // Transform the document to the result stream
    transformer.transform(domSource, result);        
    return stringWriter.toString();
}

回答by Andrzej Doyle

In this context, you'll need to convert your Documentback into some kind of stream (either character or bytes), in order for it to be processed as a StreamSource.

在这种情况下,您需要将Document返回转换为某种流(字符或字节),以便将其作为StreamSource.

If the document is small, the easiest way might simply be to convert it into a string, create a StringReaderover this and pass that reader to the StreamSource constructor. If the document is larger (or of unknown size) such that serializing it might take too much memory, you're going to have to create piped readersand writersin order to achieve the same thing (which is a pain due to the need to manage threads, but this is unavoidable if dumping the whole document temporarily is out).

如果文档很小,最简单的方法可能只是将其转换为字符串,StringReader在 this 上创建一个并将该读取器传递给 StreamSource 构造函数。如果文件较大,使得其序列可能需要太多的内存(或未知大小的),你将不得不创建管道的读者作家,以达到同样的事情(这是一种痛苦,由于需要管理线程,但如果暂时转储整个文档,这是不可避免的)。

An alternative approach might be to have a look where the Documentcame from in the first place. There's a reasonable chance that it arrived to your application as some sort of stream anyway. It maybe easier to look at this step in the logic, and get the document's raw stream again for passing into the StreamSource, rather than reserializing the Document.

另一种方法可能是首先看看Document来自哪里。无论如何,它很有可能作为某种流到达您的应用程序。它可以更容易看到这一步的逻辑,并重新获取文档的原始流传递进入StreamSource,而不是reserializing文档。

回答by Andreas Dolk

Acts as an holder for a transformation Source in the form of a stream of XML markup.

作为 XML 标记流形式的转换源的持有者。

A Streamsource wraps a stream of xml tokens. So you simply have to serializethe document to xml.

Streamsource 包装了一个 xml 令牌流。因此,您只需将文档序列化为xml。