Java Utililty Methods XML Document Save to File

List of utility methods to do XML Document Save to File

Description

The list of methods to do XML Document Save to File are organized into topic(s).

Method

voidsave(Document doc, File file)
save
save(doc, new StreamResult(file));
voidsave(Document doc, String file, String encoding)
Save the XML document to a file.
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Result result = new StreamResult(new File(file));
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
voidsave(Document document, OutputStream out, Properties outputProperties)
Writes the Document to the specified OutputStream.
saveImpl(document, new StreamResult(out), outputProperties);
voidsave(Document document, OutputStream outputStream)
save
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(outputStream);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty("indent", "yes");
transformer.transform(source, result);
outputStream.close();
Stringsave(Document document, String fileName)
save
try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileName)))) {
    save(document, out);
} catch (Exception e) {
    e.printStackTrace();
return fileName;
voidsave(Document document, String path)
Saves a DOM document to an XML file in utf-8.
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
PrintWriter pw = new PrintWriter(new FileOutputStream(path));
StreamResult destination = new StreamResult(pw);
transformer.transform(source, destination);
...
voidsave(final String path, final Document xml)
save
try {
    final Transformer tr = TransformerFactory.newInstance().newTransformer();
    tr.setOutputProperty(OutputKeys.INDENT, "yes");
    tr.setOutputProperty(OutputKeys.METHOD, "xml");
    tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    tr.transform(new DOMSource(xml), new StreamResult(new FileOutputStream(path)));
} catch (final Exception e) {
...
voidsave(String filename, Document document)
save
PrintStream out = null;
try {
    out = new PrintStream(new BufferedOutputStream(new FileOutputStream(filename)), true, "UTF-8");
    print(out, document);
} catch (Exception ex) {
    throw new IOException(ex.getLocalizedMessage());
} finally {
    if (out != null)
...
voidsaveDoc(Document doc, OutputStream output)
save Doc
try {
    Transformer transformer = TRANSFORMER_FACTORY.newTransformer();
    transformer.transform(new DOMSource(doc), new StreamResult(output));
} finally {
    try {
        output.close();
    } catch (IOException e) {
voidsaveDoc(Document doc, String fileName)
save Doc
try {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(fileName));
    transformer.transform(source, result);
} catch (TransformerException e) {
    e.printStackTrace();
...