33

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);
Mike Pone
  • 18,705
  • 13
  • 53
  • 68

5 Answers5

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());
Gary Kephart
  • 4,860
  • 5
  • 39
  • 52
  • you used the 1st line of code in the last line. the middle lines did not do anything if u check it ... – AbhishekB Sep 27 '12 at 12:23
  • 3
    The first line used ByteArrayOUTPUTStream while the last used ByteArrayINPUTStream. Futhermore, outputStream declared in the first line is used as a param to StreamResult. – Gary Kephart Dec 10 '12 at 21:56
  • but fomsource doesn't accept document. casting doc to Node doesn't work as well. any suggestions? – Ronald Sep 29 '18 at 07:40
  • Document is a subinterface of Node, so you should be able to use Document as is. See: https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Document.html – Gary Kephart Oct 01 '18 at 17:41
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());
}
Haroldo Gondim
  • 7,725
  • 9
  • 43
  • 62
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.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Giordano Maestro
  • 341
  • 1
  • 5
  • 9
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.

Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
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!

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Kostadinov
  • 71
  • 7