check the compatibility of an XML document against a DTD using JDOM?

折月煮酒 提交于 2019-12-02 08:47:56

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

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);

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!