how to create an InputStream from a Document or Node
Asked Answered
T

5

32

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.

Document doc = getDocument();
InputStream is = ???;
MyObject obj = (MyObject) xstream.fromXML(is);
Trigeminal answered 14/5, 2009 at 18:47 Comment(0)
E
60
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());
Escarole answered 14/5, 2009 at 18:59 Comment(4)
you used the 1st line of code in the last line. the middle lines did not do anything if u check it ...Catalase
The first line used ByteArrayOUTPUTStream while the last used ByteArrayINPUTStream. Futhermore, outputStream declared in the first line is used as a param to StreamResult.Escarole
but fomsource doesn't accept document. casting doc to Node doesn't work as well. any suggestions?Equality
Document is a subinterface of Node, so you should be able to use Document as is. See: docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.htmlEscarole
V
9

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

/*
 * 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());
}
Velleman answered 18/8, 2009 at 8:5 Comment(0)
M
3

One way to do it: Adapt the Document to a Source with DOMSource. Create a StreamResult to adapt a ByteArrayOutputStream. Use a Transformer from TransformerFactory.newTransformer to copy across the data. Retrieve your byte[] and stream with ByteArrayInputStream.

Putting the code together is left as an exercise.

Maeda answered 14/5, 2009 at 18:55 Comment(0)
S
3
 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.

Seve answered 7/12, 2012 at 13:46 Comment(0)
L
2
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!

Lhary answered 26/8, 2014 at 8:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.