[JAXP] Trying to avoid downloading DTD file once for each new file parsed

Hello all,

I'm currently learning JAXP & DOM parsing. And I must admit that it's pretty nice. But I've got only two little problems left.

OK, for the first one : I'm used to parse several XHTML files one after the other and in the process, as soon as I open a new file and parse it (code following), it downloads the corresponding DTD :

*********************

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

factory.setValidating(false);

DocumentBuilder builder= factory.newDocumentBuilder();

Document doc = builder.parse(myFile);

*********************

But I find it a little bit annoying because a) all my files use the same DTD and b) downloading it and all its inline linked resources is a rather long process.

So my question is : is there any way not to download the DTD each time I parse a new file ? I mean : downloading it only once would be great. Caching, maybe or anything else ? I've tried not to create a factory and a builder each time, but unfortunately, it seems that the retrieving of the DTD is a part of the parse() call itself...

Cheers !

[1200 byte] By [AlienQueena] at [2007-11-27 11:14:00]
# 1

Sure, you could cache it if you liked. An org.xml.sax.EntityResolver would be the tool for that.

DrClapa at 2007-7-29 14:04:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

OK, I did it. I put my code here, if anyone is interested :

***************************************

factory = DocumentBuilderFactory.newInstance();

factory.setNamespaceAware(true);

factory.setValidating(false);

builder = factory.newDocumentBuilder();

builder.setEntityResolver(new DTDResolver());

Document doc = builder.parse(f);

***************************************

class DTDResolver implements EntityResolver {

public InputSource resolveEntity(String publicID, String systemID) {

if (systemID.equals("http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd")) {

return new InputSource(

"file:///xhtml11.dtd");

} else if (publicID.equals("-//W3C//ELEMENTS XHTML Inline Style 1.0//EN")) {

return new InputSource(

"file:///xhtml-inlstyle-1.mod");

} else if (publicID.equals( // and so on for each resource included in the DTD (which are on the w3.org server)

}

}

***************************************

It definitely worths the price : my parsing is 3 up to 10 times faster now.

Cheers !

AlienQueena at 2007-7-29 14:04:26 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...