How to Solve DTD Validation Problem When Parsing XML Files

I think many people, you can also count on me, think that, in order to prevent getting exceptions when DTD specified in XML file to be parsed is unaccessible, setting validation feature of an XML parser to false is the way to go. Unfortunately, it is simply wrong! Actually, the fact that we don’t request validation is irrelevant, XML parser still requires it to expand any entity references. I think, the reason behind this so common misconception is partly because DTD is used to validate XML files. You need to either edit DTD reference out of file before parsing begins, or create a custom EntityResolver to satisfy the entity reference requests. Below is not working for parsing XML files which have unaccessible DTDs:


And this is a code piece that brings a solution to the problem with defining a custom EntityResolver, and configuring it to parser:

public class HibernateMappingDTDEntityResolver implements EntityResolver {
     private InputSource source;     
     HibernateMappingDTDEntityResolver () throws SAXException {
         Resource dtd = new ClassPathResource("org/hibernate/hibernate-mapping-3.0.dtd");
         try {
             source = new InputSource(dtd.getInputStream());         
         } catch (IOException e) {
             throw new SAXException(e);         

    public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
          return source;     

Resource xmlFileResource = new ClassPathResource("test.xml");
XMLReader reader = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
reader.setEntityResolver(new HibernateMappingDTDEntityResolver());
reader.parse(new InputSource(xmlFileResource.getInputStream()));

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.