Here is a more elegant way to serialize an xml dom to file in Java than using a Transformer. I've heard that LSSerializer also out-performs the usual Transformer approach, but I haven't verified this so far. The source code is HERE.
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.xml.parsers.DocumentBuilderFactory;
public class XmlSerializer {
public static void main(String[] args) throws Exception {
String xml = "ROOT(PARAMS,PARAM1,PARAM2,PARAM3)";
Scanner s = new Scanner(xml);
s.useDelimiter("[(),\\s]");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Node root = doc.createElement(s.next());
doc.appendChild(root);
Node sheet = doc.createElement(s.next());
root.appendChild(sheet);
while (s.hasNext()) {
Node param = doc.createElement(s.next());
sheet.appendChild(param);
}
// Serialize to file in current directory
XmlSerializer.saveToFile(doc, "x.xml");
}
public static void saveToFile(Document doc, String fileName) {
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
// Absence of the following line causes a buggy xml declaration (no terminating line separator)
lsSerializer.getDomConfig().setParameter("http://www.oracle.com/xml/jaxp/properties/isStandalone", Boolean.TRUE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = domImplementation.createLSOutput();
try (Writer out = Files.newBufferedWriter(Path.of(fileName))) {
output.setEncoding("UTF-8");
output.setCharacterStream(out);
lsSerializer.write(doc, output);
}
} catch (IOException e) {
throw new RuntimeException("Error serializing document", e);
}
}
}
import org.w3c.dom.Node;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.xml.parsers.DocumentBuilderFactory;
public class XmlSerializer {
public static void main(String[] args) throws Exception {
String xml = "ROOT(PARAMS,PARAM1,PARAM2,PARAM3)";
Scanner s = new Scanner(xml);
s.useDelimiter("[(),\\s]");
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Node root = doc.createElement(s.next());
doc.appendChild(root);
Node sheet = doc.createElement(s.next());
root.appendChild(sheet);
while (s.hasNext()) {
Node param = doc.createElement(s.next());
sheet.appendChild(param);
}
// Serialize to file in current directory
XmlSerializer.saveToFile(doc, "x.xml");
}
public static void saveToFile(Document doc, String fileName) {
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
// Absence of the following line causes a buggy xml declaration (no terminating line separator)
lsSerializer.getDomConfig().setParameter("http://www.oracle.com/xml/jaxp/properties/isStandalone", Boolean.TRUE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = domImplementation.createLSOutput();
try (Writer out = Files.newBufferedWriter(Path.of(fileName))) {
output.setEncoding("UTF-8");
output.setCharacterStream(out);
lsSerializer.write(doc, output);
}
} catch (IOException e) {
throw new RuntimeException("Error serializing document", e);
}
}
}