Ejemplo n.º 1
0
  /**
   * Write the document object to a file.
   *
   * @param document the document object.
   * @param filePathname the path name of the file to be written to.
   * @param method the output method: for instance html, xml, text
   * @param indent amount of indentation. -1 to use the default.
   * @throws TransformerException if an exception occurs.
   * @throws IOException if an IO exception occurs.
   */
  public static void writeDocumentToFile(
      Document document, String filePathname, String method, int indent)
      throws TransformerException, IOException {

    checkFolderForFile(filePathname);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
    transformer.setOutputProperty(OutputKeys.METHOD, method);

    if (indent > -1) {
      transformer.setOutputProperty(
          org.apache.xml.serializer.OutputPropertiesFactory.S_KEY_INDENT_AMOUNT,
          Integer.toString(indent));
    }
    transformer.transform(
        new DOMSource(document), new StreamResult(new FileOutputStream(filePathname)));
  }