1

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-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)));
}
Anand
  • 11,872
  • 10
  • 39
  • 51
  • this may be some help for u: http://stackoverflow.com/questions/1096365/validate-an-xml-file-against-local-dtd-file-with-java – Alvin Jul 07 '11 at 11:22

1 Answers1

2

You have to implement the EntityResolver, checkout this example.

Sachin Mhetre
  • 4,465
  • 10
  • 43
  • 68