I am working with Java and I want to check if an XML file is valid against a DTD.
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.
Is there any way to do it using JDOM?
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.
来源:https://stackoverflow.com/questions/29086180/check-the-compatibility-of-an-xml-document-against-a-dtd-using-jdom