XML validation using dtd
I am using the following snippet to write an XML file. I need to throw an exception if the document is not valid one. How can I do validation in this piece of code?
private static void writeToFile(Node node, File file) throws Exception {
TransformerFactory tf = TransformerFactory.newInstance();
tf.setAttribute("indent-n开发者_运维知识库umber", 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)));
}
You have to implement the EntityResolver, checkout this example.
精彩评论