0

I have added <!DOCTYPE testElement SYSTEM "http://www.example.com/test.dtd"> and write below mentioned java code to validate but it gives error for every tag but if change the dtd path to local it works fine. Cant we validate with url based dtd?

          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          factory.setValidating(true);
          factory.setNamespaceAware(true);

          DocumentBuilder builder = factory.newDocumentBuilder();

          builder.setErrorHandler(
              new ErrorHandler() {
                public void warning(SAXParseException e) throws SAXException {
                  System.out.println("WARNING : " + e.getMessage()); // do nothing
                }

                public void error(SAXParseException e) throws SAXException {
                  System.out.println("ERROR : " + e.getMessage());
                  throw e;
                }

                public void fatalError(SAXParseException e) throws SAXException {
                  System.out.println("FATAL : " + e.getMessage());
                  throw e;
                }
              }
              );
          builder.parse(new InputSource(xml));
          return true;
potame
  • 7,597
  • 4
  • 26
  • 33
amit
  • 89
  • 1
  • 13
  • You should use an XML Catalog to resolve URLs against a local DTD or Schema. See for example http://xerces.apache.org/xerces2-j/faq-xcatalogs.html. – potame Jun 13 '20 at 06:32
  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include the source code you have as a [mcve], which can be compiled and tested by others. Validation against an external DTD works without problem. Include the XML content and the URL to your DTD file as well to your question. – Progman Jun 13 '20 at 08:46

0 Answers0