Thread-safe SAX parsing (was Re: Java based XML parsers for server use.)

David Megginson david at
Fri May 7 13:51:38 BST 1999

Lars Marius Garshol writes:

 > * mads at
 > |
 > | Are there any Java based XML parsers that are suitable for server
 > | side use? One of the primary requirements for such a parser is that
 > | should be thread safe. i.e.., more than one thread should be able to
 > | use the parser simultaneously.
 > I have to confess that I haven't looked at this aspect of parsers


You could start with this:

import java.util.Locale;

import org.xml.sax.Parser;
import org.xml.sax.InputSource;
import org.xml.sax.EntityResolver;
import org.xml.sax.DTDHandler;
import org.xml.sax.DocumentHandler;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;

public class SafeParser implements Parser
  public SafeParser (Parser parser)
    this.parser = parser;

  public synchronized void setLocale (Locale locale)
    throws SAXException

  public synchronized void setEntityResolver (EntityResolver resolver)

  public synchronized void setDTDHandler (DTDHandler handler)

  public synchronized void setDocumentHandler (DocumentHandler handler)

  public synchronized void setErrorHandler (ErrorHandler handler)

  public synchronized void parse (InputSource source)
    throws SAXException, IOException

  public synchronized void parse (String systemId)
    throws SAXException, IOException

  private Parser parser;

Although I am far from an expert in concurrent programming, my feeble
brain thinks that this should work provided that the following
conditions hold true:

1. the implementor of the original parser doesn't use static variables
   to store parse information -- in other words, the parser is
   reentrant (they all should be: anyone competent enough to write an
   XML parser in the first place would probably write a reentrant
   one); and 

2. no two instantiations of the parser use the same Reader or
   InputStream instantiation (that would be dumb anyway).

Once the parse begins, the SAX parser itself controls flow of
processing, so I cannot imagine how another thread could mess up the
actual parsing (there's no way to change the parser's state externally
once it begins parsing).  To be perfectly safe, you could always
synchronize on the InputSource and/or the InputStream/Reader that you
are using.

Perhaps people with more experience in Java concurrent programming can 
take (friendly) shots at this suggestion.

All the best,


David Megginson                 david at

xml-dev: A list for W3C XML Developers. To post, mailto:xml-dev at
Archived as: and on CD-ROM/ISBN 981-02-3594-1
To (un)subscribe, mailto:majordomo at the following message;
(un)subscribe xml-dev
To subscribe to the digests, mailto:majordomo at the following message;
subscribe xml-dev-digest
List coordinator, Henry Rzepa (mailto:rzepa at

More information about the Xml-dev mailing list