我正在使用以下代码段来编写XML文件。如果文档无效,我需要抛出异常。如何在这段代码中进行验证?
private static void writeToFile(Node node, File file) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute("indent-number", new Integer(4));
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "schema.dtd");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
}