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

独自空忆成欢 提交于 2019-12-20 07:26:59

问题


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?


回答1:


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

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