commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From p>
Subject Re: [digester] problem loading the dtd when using getResourceAsStream()
Date Fri, 23 May 2003 13:28:42 GMT

On Tuesday, May 20, 2003, at 05:37  PM, Craig R. McClanahan wrote:
> On Tue, 20 May 2003, p wrote:
>> Date: Tue, 20 May 2003 16:39:16 -0400
>> From: p <>
>> Reply-To: Jakarta Commons Users List <>
>> To:
>> Subject: [digester] problem loading the dtd when using
>>     getResourceAsStream()
>> Hi,
>> My XML file has a DTD declaration like this:
>> <!DOCTYPE help SYSTEM "help.dtd">
>> The DTD file is in the same directory as the XML file, which works 
>> when
>> opening the file in an XML editor. But I am loading the file in my
>> application (a web application) as a resource, like this:
>> InputStream in = getClass().getResourceAsStream(helpFileName);
>> Digester d = new Digester();
>> d.setValidating(true);
>> // ...
>> d.parse(in);
>> The error I get at runtime is:
>> org.xml.sax.SAXParseException: Relative URI "help.dtd"; can not be
>> resolved without a base URI.
> That's because the InputStream you ultimately hand in to the XML parser
> does not have any URI associated with it, so the parser has no 
> mechanism
> to resolve relative URIs.
>> I still get this error if I do setValidating(false), which I find odd.
> Even with validating turned off, the parser is still going to process 
> the
> doctype reference.
>> So my question is, what should I use for the DTD URI? Or how should I
>> be doing this differently? I can't specify an absolute URI starting
>> with a '/' because I don't know where the app will be deployed on the
>> sytem (which is why I'm using getResourceAsStream()). The DTD file is
>> available as a resource, but how can I tell the digester where to find
>> it?
>> I think I could put the DTD on a web server somewhere and use a URI
>> starting with "http://", but there are many reasons why I don't want 
>> to
>> do that, so hopefully somebody will enlighten me with a better answer!
> Another alternative is to use the org.xml.sax.InputSource mechanism to
> provide a URI for the base document.  That way, the XML parser will
> understand how to deal with relative references.  One approach (if 
> you're
> using files):
>   URL url = (new File("... path to file ...")).toURL();
>   InputSource is = new InputSource(url.toExternalForm());
>   is.setByteStream(url.openStream());
>   digester.parse(is);
> In principle, this approach works for any sort of URL that you have a
> resolver for (such as http and ftp).  The relative path "help.dtd" 
> will be
> resolved against the absolute URL being utilized.

Thanks for the help, I changed it to use getResource() instead of 
getResourceAsStream(), which returns a file:/ url, and from there it's 
like your example. Theoretically, the file that I'm finding with 
getResource() could actually be inside a jar file, instead of a file on 
the filesystem, so I don't know what would happen in that case... I'm 
not sure what type of URL would be returned and whether the digester 
would be able to resolve something relative to it, but I'm not worrying 
about it :-)


View raw message