title: Horrible Performance Parsing XHTML File with Doctype as XML Document
body: <p>When I parse this xhtml file as xml, it takes approximately 2 minutes to do the parsing on such a simple file. I have found that if I remove the doctype declaration, it parses nigh instantaneously. What is wrong that is causing this file to take so long to parse?</p>
  
  <p>Java Example</p>
  
  <pre><code>DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  dbf.setNamespaceAware( true );
  DocumentBuilder bob = dbf.newDocumentBuilder();
  Document template = bob.parse( new InputSource( new FileReader( xmlFile ) ) );
  </code></pre>
  
  <p>XHTML Example</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ex="http://www.example.com/schema/v1_0_0"&gt;
      &lt;head&gt;&lt;title&gt;Test&lt;/title&gt;&lt;/head&gt;
      &lt;body&gt;
          &lt;h1&gt;Test&lt;/h1&gt;
          &lt;p&gt;Hello, World!&lt;/p&gt;
          &lt;p&gt;&lt;ex:test&gt;Text&lt;/ex:test&gt;&lt;/p&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Thanks</p>
  
  <p><strong>Edit: Solution</strong></p>
  
  <p>To actually fix the problem based on the information provided about why it was happening in the first place, I did these basic steps:</p>
  
  <ol>
  <li>Downloaded the DTD-related files to a src/main/resources folder</li>
  <li>Created a custom EntityResolver to read these files from the classpath</li>
  <li>Told my DocumentBuilder to use my new EntityResolver</li>
  </ol>
  
  <p>I referenced this SO answer in doing so: <a href="https://stackoverflow.com/questions/5140648/how-to-validate-xml-using-java">how to validate XML using java?</a></p>
  
  <p>New EntityResolver</p>
  
  <pre><code>import java.io.IOException;
  
  import org.xml.sax.EntityResolver;
  import org.xml.sax.InputSource;
  import org.xml.sax.SAXException;
  
  public class LocalXhtmlDtdEntityResolver implements EntityResolver {
  
      /* (non-Javadoc)
       * @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String, java.lang.String)
       */
      @Override
      public InputSource resolveEntity( String publicId, String systemId )
              throws SAXException, IOException {
          String fileName = systemId.substring( systemId.lastIndexOf( "/" ) + 1 );    
          return new InputSource( 
                  getClass().getClassLoader().getResourceAsStream( fileName ) );
      }
  
  }
  </code></pre>
  
  <p>How to use new EntityResolver:</p>
  
  <pre><code>DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  dbf.setNamespaceAware( true );
  DocumentBuilder bob = dbf.newDocumentBuilder();
  bob.setEntityResolver( new LocalXhtmlDtdEntityResolver() );
  Document template = bob.parse( new InputSource( new FileReader( xmlFile ) ) );
  </code></pre>