Home / Java Patterns and Pitfalls     frequal.com

Finding a DTD within a JAR File

When an external DTD is specified in the DOCTYPE attribute of an XML file, the parse has to open the DTD externally. If the DTD URL provided is absolute, no problems occur: either the document is present at that location or it is not.

However, using a relative URL for the DTD is preferable in many cases, since the DTD and XML file may be paired on the filesystem or in a JAR file.

When invoking the parser, be sure to use the following code:

// Correctly passing the URL for the XML file to the parser
This will pass the parser the URL of the XML file. It can use that to find the DTD even if the DTD is specified with a relative URL.

In contrast, code that passes the XML file to the parser as an InputStream strips the parser of its ability to find relative URLs and will cause the parsing to fail.

// AVOID THIS: Passing XML File as input stream

Last modified on 21 Dec 2005 by AO

Copyright © 2020 Andrew Oliver