How to stop parsing xml document with SAX at any time?

Create a specialization of a SAXException and throw it (you don’t have to create your own specialization but it means you can specifically catch it yourself and treat other SAXExceptions as actual errors).

public class MySAXTerminatorException extends SAXException {
    ...
}

public void startElement (String namespaceUri, String localName,
                           String qualifiedName, Attributes attributes)
                        throws SAXException {
    if (someConditionOrOther) {
        throw new MySAXTerminatorException();
    }
    ...
}

Leave a Comment