使用 dtd 进行 XML 验证
我正在使用以下代码片段来编写 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)));
}
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)));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须实现 EntityResolver,请查看此示例。
You have to implement the EntityResolver, checkout this example.