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

voidsaveDocument(String filename, byte[] document)
save Document
Document doc = null;
try {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
    doc = docBuilder.parse(new ByteArrayInputStream(document));
} catch (Exception e) {
    String message = "Unable to save file: " + filename;
...
voidsaveDocumentToFile(Document doc, File file)
save Document To File
if (file.getParent() != null) {
    mkdirs(file.getParent());
FileOutputStream fos = new FileOutputStream(file);
Source source = new DOMSource(doc);
Result result = new StreamResult(fos);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
...
voidsaveDocumentToFile(Document doc, String filename)
save Document To File
OutputStream os = new FileOutputStream(filename);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(new DOMSource(doc), new StreamResult(os));
os.close();
booleansaveDOM(Document doc, String filename)
Saves a document object model (DOM) as a file with the chosen file name.
boolean save = false;
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
    StreamResult result = new StreamResult(filename);
...
voidSaveDOM(Document document, String filename)
This writes the DOM to an XML file
try {
    Source source = new DOMSource(document);
    File file = new File(filename);
    Result result = new StreamResult(file);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(source, result);
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
...
voidSaveDom(Document dom, String filepath)
Save Dom
try {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource domSource = new DOMSource(dom);
    StreamResult streamResult = new StreamResult(new FileOutputStream(filepath));
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.transform(domSource, streamResult);
...
voidsaveDOM(final Document doc, final OutputStream output)
Save a DOM object in a stream
final Transformer transformer = sTransformerFactory.newTransformer();
final DOMSource source = new DOMSource(doc);
final StreamResult result = new StreamResult(output);
transformer.transform(source, result);
voidsaveDomToFile(Document doc, String filePath)
Save content of DOM to a file.
Source source = new DOMSource(doc);
File file = new File(filePath);
Result result = new StreamResult(file);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);
voidsaveHumanReadable(Document document, File file)
Saves a DOM to a human-readable XML file (4-space indentation, UTF-8).
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", new Integer(4));
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8");
transformer.transform(new DOMSource(document), new StreamResult(writer));
...
voidsaveImpl(Document document, StreamResult output, Properties outputProperties)
Work method for public save() methods.
try {
    TransformerFactory tFactory = getTransformerFactory();
    Transformer transformer = tFactory.newTransformer();
    if (outputProperties != null) {
        transformer.setOutputProperties(outputProperties);
    DOMSource source = new DOMSource(document);
    transformer.transform(source, output);
...