mifflin has asked for the wisdom of the Perl Monks concerning the following question:

I'm parsing a large XML document where I only need a little bit of the beginning of the document. Is there a way to force processing to stop as if the end of the document has actually been reached and have the end_document sub in the handler package be called. Ideally I'd like to force it to happen in the start_element sub when a certain element is encountered.

Replies are listed 'Best First'.
Re: How to force end_document in XML::SAX?
by samtregar (Abbot) on Nov 10, 2004 at 17:08 UTC
    No.

    But maybe you can do something like this in your start_element():

    if ($all_done) { $self->end_document(); die "Ended early!\n"; }

    Then in your parsing code you can catch the exception with eval and handle it:

    eval { # parse call here }; if ($@ and $@ =~ /Ended early/) { # it's ok } elsif ($@) { die $@; }

    For a slightly prettier solution, create a new exception class based on XML::SAX::Exception and key on that class rather than the message in $@.

    -sam

      that'll work
      thanks.