26

I parse a big xml document with Sax, I want to stop parsing the document when some condition establish? How to do?

Diablo.Wu
  • 1,151
  • 5
  • 15
  • 17

3 Answers3

40

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();
    }
    ...
}
Tom
  • 43,583
  • 4
  • 41
  • 61
  • 3
    There any other way? not use exception. – Diablo.Wu Aug 28 '09 at 07:19
  • 2
    Why would you not want to? That's what exceptions are designed for. – Xiong Chiamiov Sep 01 '09 at 20:29
  • 22
    fwiw, that isn't what exceptions are designed for. Terminating a parse like this is not an error condition. It is however the only way to do this afaict :-( – ashirley Apr 20 '10 at 14:10
  • In fact, it should be [SAXParseException](http://www.saxproject.org/apidoc/org/xml/sax/SAXParseException.html) so you can populate a Locator, and get a callback on the ErrorHandler (which is the right place to 'tidy up'). – David Bullock Nov 23 '12 at 06:37
  • Here is good example - [Stop a SAX parser when you have enough data](https://www.ibm.com/developerworks/library/x-tipsaxstop/index.html) – Dmitry Trifonov Jul 26 '17 at 13:39
4

I am not aware of a mechanism to abort SAX parsing other than the exception throwing technique outlined by Tom. An alternative is to switch to using the StAX parser (see pull vs push).

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267
2

I use a boolean variable "stopParse" to consume the listeners since i don´t like to use throw new SAXException();

private boolean stopParse;

article.getChild("title").setEndTextElementListener(new EndTextElementListener(){
            public void end(String body) {
                if(stopParse) {
                  return; //if stopParse is true consume the listener.
                }
                setTitle(body);
            }
        });

Update:

@PanuHaaramo, supossing to have this .xml

<root>
        <article>
               <title>Jorgesys</title>
        </article>
        <article>
               <title>Android</title>
        </article>
        <article>
               <title>Java</title>
        </article>
</root>

the parser to get the "title" value using android SAX must be:

   import android.sax.Element;
   import android.sax.EndTextElementListener;
   import android.sax.RootElement;
...
...
...
    RootElement root = new RootElement("root");
    Element article= root.getChild("article");
    article.getChild("title").setEndTextElementListener(new EndTextElementListener(){
                public void end(String body) {
                    if(stopParse) {
                      return; //if stopParse is true consume the listener.
                    }
                    setTitle(body);
                }
            });
Jorgesys
  • 124,308
  • 23
  • 334
  • 268