Java 如何从文档或节点创建 InputStream

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

how to create an InputStream from a Document or Node

javaxmlxstream

提问by Mike Pone

How can I create an InputStream object from a XML Document or Node object to be used in xstream? I need to replace the ??? with some meaningful code. Thanks.

如何从要在 xstream 中使用的 XML 文档或节点对象创建 InputStream 对象?我需要更换???带有一些有意义的代码。谢谢。

Document doc = getDocument();
InputStream is = ???;
MyObject obj = (MyObject) xstream.fromXML(is);

采纳答案by Gary Kephart

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(doc);
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());

回答by Tom Hawtin - tackline

One way to do it: Adapt the Documentto a Sourcewith DOMSource. Create a StreamResultto adapt a ByteArrayOutputStream. Use a Transformerfrom TransformerFactory.newTransformerto copy across the data. Retrieve your byte[]and stream with ByteArrayInputStream.

一种方式做到这一点:适应DocumentDOMSource的。创建一个StreamResult以适应ByteArrayOutputStream。使用TransformerFactory.newTransformer 中Transformer复制数据。byte[]使用ByteArrayInputStream检索您的和流。

Putting the code together is left as an exercise.

将代码放在一起作为练习。

回答by Gary Kephart

If you are using Java without any Third Party Libraries, you can create InputStreamusing below code:

如果您在没有任何第三方库的情况下使用 Java,则可以InputStream使用以下代码创建:

/*
 * Convert a w3c dom node to a InputStream
 */
private InputStream nodeToInputStream(Node node) throws TransformerException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Result outputTarget = new StreamResult(outputStream);
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), outputTarget);
    return new ByteArrayInputStream(outputStream.toByteArray());
}

回答by Giordano Maestro

 public static InputStream document2InputStream(Document document)    throws IOException {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      OutputFormat outputFormat = new OutputFormat(document);
      XMLSerializer serializer = new XMLSerializer(outputStream, outputFormat);
      serializer.serialize(document);
      return new ByteArrayInputStream(outputStream.toByteArray());
 }

This works if you are using apache Xerces implementation, you can also set format parameter with the output format.

如果您使用的是 apache Xerces 实现,这有效,您还可以使用输出格式设置格式参数。

回答by NikolayKostadinov

public static InputStream documentToPrettyInputStream(Document doc) throws IOException {

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    XMLWriter xmlWriter = new XMLWriter(outputStream, OutputFormat.createPrettyPrint());
    xmlWriter.write(doc);
    xmlWriter.close();

    InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

    return inputStream;
}      

If you happen to use DOM4j and you need to print it pretty!

如果您碰巧使用 DOM4j 并且需要漂亮地打印它!