RE: How to dynamically specify a path to a DTD file

Dear Zeljko,

> I would like to specify dynamically the path of a DTD file to 
> the XML parser
> : I've include a DOCTYPE declaration with a SYSTEM call to a 
> relative URL.
> The implementation of the DOMParser I'm using is Xerces for 
> Java. I would
> like to know if there is a way to resolve the relative URL of 
> the SYSTEM
> call programatically ? Much better : I'd like also, if it's 
> possible, to
> create the DTD InputSource myself, so as to keep full control 
> upon input and
> output in my program. Thanks for your help.

If I understand your question, you are looking for an entity resolver. 
You have these both on Xerces's own DOMParser (EntityResolver inteface
taken from SAX) as well as Load/ Save which Xerces also supports
(DOMEntityResolver). Since you already already mention DOMParser, I will
give an example with that interface (although I should plug W3C's Load/
Save of course):

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

    DOMParser myParser = new DOMParser();
    myParser.setEntityResolver(new EntityResolver() {
      public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
        if ((systemId != null) && systemId.endsWith("mydtd.dtd") {
          return new InputSource(new StringReader("<!ELEMENT dummy
EMPTY>"));
        } else {
          return null;
        }
      }
    });

Kidn regards,

--Sander.

Received on Friday, 11 October 2002 03:49:34 UTC