Java XML Document to File writeTo(Document document, File output)

Here you can find the source of writeTo(Document document, File output)

Description

write To

License

Apache License

Declaration

public static void writeTo(Document document, File output) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Main {
    public static void writeTo(Document document, File output) {
        try {/* www .j  a v  a2s  . co m*/
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);

            FileOutputStream outputstream = new FileOutputStream(output);
            StreamResult result = new StreamResult(outputstream);

            // Manually add xml declaration, to force a newline after it.
            String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
            outputstream.write(xmlDeclaration.getBytes());

            // Remove whitespaces outside tags.
            // Essential to make sure the nodes are properly indented.
            XPath xPath = XPathFactory.newInstance().newXPath();
            NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document,
                    XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); ++i) {
                Node node = nodeList.item(i);
                node.getParentNode().removeChild(node);
            }

            // Pretty-print options.
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            transformer.transform(source, result);

            outputstream.close();
        } catch (TransformerException | IOException | XPathExpressionException e) {
            System.out.println("Failed to write document file" + output.getPath() + ": " + e.toString());
        }
    }
}

Related

  1. writeDomToFile(Document dom, File outFile)
  2. writeDomToFile(Document dom, File outFile)
  3. writeDomToFile(Document dom, File outFile)
  4. writeDomToFile(String filename, Document document)
  5. writeFile(String fileName, Document content)
  6. writeToFile(Document doc, String file)
  7. writeToFile(Document doc, String filePath)
  8. writeToFile(File file, Document doc)
  9. writeXML(Document doc, File file)