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.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
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");
}
/**
*
* @param doc The Document to serialize to String
* @param fileName The file path to which to write
*/
public static void saveToFile(Document doc, String fileName) {
FileWriter out = null;
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
lsSerializer.getDomConfig()
.setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = domImplementation.createLSOutput();
output.setEncoding(System.getProperty("file.encoding"));
out = new FileWriter(fileName);
output.setCharacterStream(out);
lsSerializer.write(doc, output);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) { /* ignore */
}
}
}
}
}
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.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Scanner;
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");
}
/**
*
* @param doc The Document to serialize to String
* @param fileName The file path to which to write
*/
public static void saveToFile(Document doc, String fileName) {
FileWriter out = null;
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
lsSerializer.getDomConfig()
.setParameter("format-pretty-print", Boolean.TRUE);
LSOutput output = domImplementation.createLSOutput();
output.setEncoding(System.getProperty("file.encoding"));
out = new FileWriter(fileName);
output.setCharacterStream(out);
lsSerializer.write(doc, output);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) { /* ignore */
}
}
}
}
}