how to create an InputStream from a Document or Node

前端 未结 5 403
后悔当初
后悔当初 2021-01-03 20:35

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.



        
5条回答
  •  醉酒成梦
    2021-01-03 20:46

     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.

提交回复
热议问题