[jdom-interest] org.xml.sax.SAXParseException: Relative URI "office.dtd"; can not be resolved without a base URI.

Laurent Bihanic laurent.bihanic at atosorigin.com
Fri Nov 29 04:43:32 PST 2002


Hemanth Raju wrote:
> 	Whenever I am tring to build the Document Object from the XML file using
> the DOMBuilder or SAXBuilder its throwing a SAXParseException.
> 
> My code is as follows:
> 
> 	DOMBuilder domBuilder = new DOMBuilder();
> 	Document doc = domBuilder.build(new
> URL("file:///d:/XMLTest/xml/content.xml"));
> 	System.out.println("Root Element = " + doc.getRootElement().getName());
> 
> and the exception is as follows:
> 
> Root cause: org.xml.sax.SAXParseException: Relative URI "office.dtd"; can
> not be resolved without a base URI.

Your XML document makes reference to a DTD through a relative URI or path and 
the XML parser is unable to resolve this relative URI from the XML document 
path, i.e. relatively to "d:/XMLTest/xml".

There are several solutions to your problem :

1. Simple: Make sure the DTD file can be found locally, relatively to the XML 
file path. If necessary, if you are using a stream/reader as input, you can 
use SAXBuilder.build(InputStream/Reader, String) or set the System ID in the 
SAX InputSource to let the parser know where the XML document actually lies 
(this corresponds to setting the "base URI" the parser is asking for).

2. More generic: Implement an org.xml.sax.EntityResolver and register it onto 
SAXBuilder to request the parser to delegate to your application the 
resolution of enternal entities. That way, you are free to store the DTD files 
whereever you wish (e.g. in the application JAR) or manage a cache of 
frequently accessed DTDs.

Laurent




More information about the jdom-interest mailing list