W3C home > Mailing lists > Public > www-dom@w3.org > October to December 2002

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

From: Sander Bos <sander@x-hive.com>
Date: Fri, 11 Oct 2002 09:45:56 +0200
Message-ID: <41D11F414A26E942912B7E7696DC8E2206161E@JAKARTA.xhive.archipel>
To: <www-dom@w3.org>



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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:56 GMT