code: public static InputStream DocumentToInputStream(Document edoc)
              throws IOException {
  
          // Create the input and output for use in the transformation
          final org.w3c.dom.Document doc = edoc;
          final PipedOutputStream pos = new PipedOutputStream();
          PipedInputStream pis = new PipedInputStream();
          pis.connect(pos);
  
          (new Thread(new Runnable() {
  
              public void run() {
                  // Use the Transformer.transform() method to save the Document
                  // to a StreamResult
                  try {
                      TransformerFactory tFactory = TransformerFactory.newInstance();
                      // Fortify Mod: prevent external entity injection
                      tFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                      Transformer transformer = tFactory.newTransformer();
                      transformer.setOutputProperty("encoding", "UTF-8");
                      transformer.setOutputProperty("indent", "yes");
                      transformer.transform(new DOMSource(doc), new StreamResult(
                              pos));
                  } catch (Exception e) {
                      throw new RuntimeException(
                              "Error converting Document to InputStream.  "
                                      + e.getMessage());
                  } finally {
                      try {
                          pos.close();
                      } catch (IOException e) {
  
                      }
                  }
              }
          }, "IOUtils.DocumentToInputStream(Document edoc)")).start();
  
          return pis;
      }
docs: Converts an org.w3c.dom.Document element to an java.io.InputStream.
  
  @param edoc
  the org.w3c.dom.Document to be converted
  @return InputStream the InputStream value of the passed doument