Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从文档或节点创建InputStream_Java_Xml_Xstream - Fatal编程技术网

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

Java 如何从文档或节点创建InputStream,java,xml,xstream,Java,Xml,Xstream,如何从要在xstream中使用的XML文档或节点对象创建InputStream对象?我需要换新的???用一些有意义的代码。谢谢 Document doc = getDocument(); InputStream is = ???; MyObject obj = (MyObject) xstream.fromXML(is); 一种方法是:将文档调整为一个带有。创建一个以适应。使用“发件人”跨数据进行复制。检索您的字节[]并使用流式处理 将代码放在一起作为练习。如果您使用的Java没有任何第三方库

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

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

一种方法是:将
文档
调整为一个带有。创建一个以适应。使用“发件人”跨数据进行复制。检索您的
字节[]
并使用流式处理


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

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

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());
/*
 * 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());
}
如果您使用的是ApacheXerces实现,则可以使用输出格式设置format参数

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

如果您碰巧使用DOM4j,并且需要将其打印得漂亮

您使用了最后一行中的第一行代码。如果你检查一下,中间的几行没有任何作用……tearrayoutputstream使用的第一行,而tearrayinputstream使用的最后一行。此外,第一行中声明的outputStream用作StreamResult的参数。非常感谢!这正是我要找的!但fomsource不接受该文档。将文档强制转换为节点也不起作用。有什么建议吗?文档是节点的子接口,所以您应该能够按原样使用文档。见:
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;
}