开发者

XML validation using dtd

开发者 https://www.devze.com 2023-03-17 17:01 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消