This post is just a placeholder so I can google for this SAX error later and remember how I fixed it.
You get one of these errors from SAX when you have an XML document that uses a relative reference to its DTD. So, say you’ve got a filter-config.xml
which contains something like:
<!DOCTYPE security PUBLIC "-//Bytecode Pty Ltd//Spam Filtering Components//EN" **"filter-config.dtd"** >
And you’ve put filter-config.dtd
in the same directory as filter-config.xml
. Fair enough. The only problem is that when you try and feed it to SAX, you get shafted with an error like:
org.xml.sax.SAXParseException: Relative URI "yourfile.dtd"; can not be resolved without a document URI.
Which wasn’t really what you had in mind. The problem here is that you are trying to access the file like this:
xmlReader.parse(new InputSource(new FileReader(configFile)));
Which SAX thinks is unreasonable since it can’t work out where exactly things are supposed to be loaded from (in network terms).
What you want to do is to feed a URL to your InputSource, with something like this:
URL configUrl = new File(configFile).toURL(); InputSource is = new InputSource(configUrl.toString()); xmlReader.parse(is);
SAX can use the URL in resolving the DTD reference and life is good again. So if you’re having problems with finding your DTD (even though it’s in the same directory), go the URL path.