OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: load DTD in memory.

Well, here is a sample class for a in-memory entity cache (it will therefore
cache DTDs). It is a dumb cache, because if the original DTD changes, the
cache is not refreshed, but for many cases it is sufficient. We have
implemented a much more flexible caching framework, but here it's out of

import java.net.*;
import java.util.*;
import org.xml.sax.*;

public class DTDMemoryCache implements EntityResolver {
    private Map cache=new HashMap();

    // Remember to handle synchronization issues !!
    // A dumb but easy implementation would synchronize the whole
    // resolveEntity method
    public synchronized InputSource resolveEntity(String publicIdP,String
systemIdP) throws SAXException {
        // The PUBLIC id is the key to our cache
        byte[] resultP=cache.get(publicIdP);

        if(resultP==null) {
            try {
			// The SYSTEM id is the URL used to fetch the entity
			URL urlP=new URL(systemIdP);
			InputStream isP=urlP.openConnection();
			ByteArrayOutputStream baosP=new

			// We copy the input stream into the output stream
			// Fast buffer implementation
			// I could have used BufferInputStream and
			// But it's much slower
			int readP;
			byte[] bufferP=new byte[1024];
			while((readP=isP.read(bufferP))>-1) {

			// We store the result in the cache.
            catch(Exception eP) {
                throw new SAXException(eP);

        return new InputSource(new ByteArrayInputStream(resultP));

Now, how to install this EntityResolver ? Both the org.xml.sax.Parser and
the javax.xml.parsers.DocumentBuilder have a setEntityResolver() method that
enables you to install your own EntotyResolver before parsing your


-----Message d'origine-----
De : Giuseppe Sarno [mailto:gsarno@nortelnetworks.com]
Envoyé : mercredi 5 septembre 2001 14:47
À : Nicolas LEHUEN; 'xml-dev@lists.xml.org'
Objet : RE: load DTD in memory.

thanks for your answer,
but could you please tell me more on how to do it since the API doc doesn't
explain it very well.

-----Original Message-----
From: Nicolas LEHUEN [mailto:nicolas.lehuen@ubicco.com]
Sent: 05 September 2001 13:26
To: Sarno, Giuseppe [MAIFP:GM12:EXCH]; 'xml-dev@lists.xml.org'
Subject: RE: load DTD in memory.

To cache DTDs the easiest thing to do with SAX is to implement your own
EntityResolver. This way you'll be able to cache locally or in memory all
entities, amongst them the DTDs.

-----Message d'origine-----
De : Giuseppe Sarno [mailto:gsarno@nortelnetworks.com]
Envoyé : mercredi 5 septembre 2001 13:56
À : xml-dev@lists.xml.org
Objet : load DTD in memory.

Hi ,
I was wondering ,
in case is needed to parse a lot of XML docs , is it possible to
load the DTD or Schema one in memory and always reuse it instead of
reloading it for each Doc validation ?