[jdom-interest] Building Document from a String

Laurent Bihanic laurent.bihanic at atosorigin.com
Mon Dec 9 08:19:13 PST 2002


Raghav Rao wrote:
> hi,
>   I have been using JDOM for my application. Whenever i try to create a
> Document using the SAXBuilder.build(java.io.Reader characterStream) method,
> i get an error saying that the dtd is not found. But if i use
> SAXBuilder.build(java.io.File file) it works fine. I need the validation to
> be turned on.
> 
> How to get around the problem?

When you use SAXBuilder.build(java.io.File file), the parser is able to 
resolve the DTD reference relatively to the file location. On the other hand, 
when using SAXBuilder.build(java.io.Reader characterStream), the parser has no 
base URI to resolve the DTD location from.

One solution is to use SAXBuilder.build(java.io.Reader characterStream, 
java.lang.String) and pass as second argument the base URI for resolving the DTD.
Another solution is to implement the org.xml.sax.EntityResolver interface to 
resolve the DTD reference by yourself and register your implementation on 
SAXBuilder using setEntityResolver(). With this solution, the DTD could be 
stored anywhere, locally or on the net, and you could also cache DTD 
definitions to speed up parsing.

Laurent




More information about the jdom-interest mailing list