使用 JDOM 检查 XML 文档与 DTD 的兼容性? [英] check the compatibility of an XML document against a DTD using JDOM?

查看:25
本文介绍了使用 JDOM 检查 XML 文档与 DTD 的兼容性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Java,我想检查 XML 文件是否对 DTD 有效.

I am working with Java and I want to check if an XML file is valid against a DTD.

假设我们确实有一个 DTD 文件,我想根据 DTD 文件中提出的相同定义检查某个 XML 文件是否有效.

Assuming that we do have a DTD file, I want to check if certain XML file is valid against the same definitions proposed in the DTD file.

有没有办法使用 JDOM 来做到这一点?

Is there any way to do it using JDOM?

推荐答案

是的,您可以,在 JDOM 中.在 JDOM 2.x 中最简单(与 1.x 相反).

Yes, you can, in JDOM. It's easiest in JDOM 2.x (as opposed to 1.x).

参见 采用 XMLReaderJDOMFactory 的 SAXBuilder 构造函数.一个示例用法是:

See the SAXBuilder constructor that takes an XMLReaderJDOMFactory. An example usage is:

    SAXBuilder sb = new SAXBuilder(XMLReaders.DTDVALIDATING);
    Document doc = sb.build("http://www.w3schools.com/dtd/note_ex_dtd.xml");
    XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
    xout.output(doc, System.out);

请注意,上述代码根据 DocType 声明(note.dtd")中指定的 DTD 验证了 XML,该 DTD 位于相对于 note-ex_dtd.xml 文档的位置.

Note that the above code validated the XML against the DTD specified in the DocType Declaration ("note.dtd") which is at a location relative to the note-ex_dtd.xml document.

这篇关于使用 JDOM 检查 XML 文档与 DTD 的兼容性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆