I happened to send a message to this list asking this exact question earlier this week. I figure I'll pass on the kindness someone showed me and answer it this time/ (Perhaps this should go in the FAQ because it isn't really intuitive.)
final static String ENTITY_RESOLVER_2_ID = " http://xml.org/sax/features/use-entity-resolver2"; DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); dbFactory.setFeature(ENTITY_RESOLVER_2_ID, true); DocumentBuilder builder = dbFactory.newDocumentBuilder(); builder.setEntityResolver(new DTDEntityResolver(new FileReader(dtdFilename))); public class DTDEntityResolver implements EntityResolver2 { InputSource input; public DTDEntityResolver(Reader dtd) { input = new InputSource(dtd); } public InputSource getExternalSubset(String name, String baseURI) throws SAXException { return input; } public InputSource resolveEntity(String publicId, String systemId) { log.debug("Entity: " + publicId + " : " + systemId); return null; } public InputSource resolveEntity(String name, String publicId, String baseURI, String systemId) throws SAXException { log.debug("Entity: " + name + ": " + baseURI + " [" + publicId + " : " + systemId + "]"); return null; } } On 2/25/07, Mike O'Leary <[EMAIL PROTECTED]> wrote:
I have a project in which I am supposed to write a parser in Java for a set of files that don't contain <!DOCTYPE ...> lines. A DTD file was defined for these files, and the files contain entity references whose definitions are in the DTD file. I looked at the JAXP documentation, and it looks like I have several options: There appear to be a variety of ways I can provide a system id to a factory object, to a parser object, or as an argument to the parse function. Or I can specify an entityResolver function for the parser, or I can tell the parser not to expand entity references. I have tried several of these, and I haven't gotten any of them to work yet. In each case, it appears that the DTD file is not read and the parse crashes with an error saying that an entity was referenced that was not declared. Can anyone provide me with a sort of recipe for constructing a parser in which the DTD file is specified without the use of a <!DOCTYPE ... > line in the file to be parsed, and the DTD file is read and is available to resolving entity references that are encountered during the parse? Thanks. Mike O'Leary