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

18,427

Solution 1

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();
    }
    ...
}

Solution 2

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).

Solution 3

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);
                }
            });
Share:
18,427

Related videos on Youtube

Diablo.Wu
Author by

Diablo.Wu

Updated on March 27, 2020

Comments

  • Diablo.Wu
    Diablo.Wu over 4 years

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

  • Diablo.Wu
    Diablo.Wu almost 15 years
    There any other way? not use exception.
  • Xiong Chiamiov
    Xiong Chiamiov almost 15 years
    Why would you not want to? That's what exceptions are designed for.
  • ashirley
    ashirley about 14 years
    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 :-(
  • David Bullock
    David Bullock over 11 years
    In fact, it should be SAXParseException so you can populate a Locator, and get a callback on the ErrorHandler (which is the right place to 'tidy up').
  • Panu Haaramo
    Panu Haaramo over 9 years
    Is this for Android? I can only find android.sax.EndTextElementListener. Also what is article here?
  • Panu Haaramo
    Panu Haaramo over 9 years
    Thanks. I'm not developing for Android but clears it up! I guess this is then not doable in regular Java (which I'm using).
  • Dmitry Trifonov
    Dmitry Trifonov almost 7 years