title: How to validate an XML file against a given DTD file?
body: <p>I have an XML file, which has a DTD reference in it, like this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE something SYSTEM "something.dtd"&gt;
  </code></pre>
  
  <p>I'm using a <code>DocumentBuilderFactory</code>:</p>
  
  <pre><code>public static Document validateXMLFile(String xmlFilePath) throws ParserConfigurationException, SAXException, IOException {
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
      domFactory.setValidating(true);
      DocumentBuilder builder = domFactory.newDocumentBuilder();
      builder.setErrorHandler(new ErrorHandler() {
          @Override
          public void error(SAXParseException exception) throws SAXException {
              // do something more useful in each of these handlers
              exception.printStackTrace();
          }
  
          @Override
          public void fatalError(SAXParseException exception) throws SAXException {
              exception.printStackTrace();
          }
  
          @Override
          public void warning(SAXParseException exception) throws SAXException {
              exception.printStackTrace();
          }
      });
      Document doc = builder.parse(xmlFilePath);
      return doc;
  }
  </code></pre>
  
  <p>But now I want to validate the XML file against a DTD file on a user-defined location, and not relative to the path of the XML file.</p>
  
  <p>How can I do that?</p>
  
  <p>Example:</p>
  
  <pre><code>validateXMLFile("/path/to/the/xml_file.xml", "/path/to/the/dtd_file.dtd");
  </code></pre>