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

voidsaveReportToFile(Node node, String documentFileName)
save Report To File
File output = new File(documentFileName);
StreamResult resultFile = new StreamResult(output);
DOMSource source = new DOMSource(node);
try {
    Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.setOutputProperty(OutputKeys.METHOD, "xml");
...
voidsaveTemporaryDocument(Document document, String folderName)
save Temporary Document
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
String str = writer.writeToString(document);
str = str.replace("UTF-16", "UTF-8");
File outputFolder = new File(folderName);
outputFolder.mkdirs();
String fileName = outputFolder.getAbsolutePath() + "/" + "file_" + String.valueOf(index) + ".xml";
...
voidsaveToDisk(Document doc, OutputStream out)
save To Disk
Transformer serializer = TransformerFactory.newInstance().newTransformer();
serializer.transform(new DOMSource(doc), new StreamResult(out));
voidsaveToFile(Document doc, File f, boolean indent)
save To File
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
try {
    int n = indent ? 4 : 0;
    writeByDom(doc, writer, n);
} finally {
    writer.close();
FilesaveToFile(String filename, Document document)
save To File
Source source = new DOMSource(document);
File file = new File(filename);
Result result = new StreamResult(file);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
return file;
...
voidsaveToXml(Document doc, OutputStream os)
save To Xml
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(os);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
StringsaveToXmlStr(Document doc)
serializing a XML DOM Document into XML String
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
...
voidsaveXML(Document doc, ByteArrayOutputStream outputStreamXML)
Serializa un XML a ByteArrayOutputStream
System.setProperty(DOMImplementationRegistry.PROPERTY, "org.apache.xerces.dom.DOMImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementation domImpl = registry.getDOMImplementation("LS 3.0");
DOMImplementationLS implLS = (DOMImplementationLS) domImpl;
LSSerializer dom3Writer = implLS.createLSSerializer();
LSOutput output = implLS.createLSOutput();
output.setByteStream(outputStreamXML);
output.setEncoding("UTF-8");
...
booleansaveXML(Document doc, File outfile, boolean indent)
save XML
try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"));
    if (indent) {
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPathExpression xpathExp = xpathFactory.newXPath().compile("//text()[normalize-space(.) = '']");
        NodeList emptyTextNodes = (NodeList) xpathExp.evaluate(doc, XPathConstants.NODESET);
...
voidsaveXml(Document doc, String filename)
save Xml
try {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    DOMSource source = new DOMSource(doc);
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
    StreamResult result = new StreamResult(pw);
    transformer.transform(source, result);
...