in reply to Modifying Records with XML::SAX::ByRecord

You're on the right track. I've never used XML::SAX::ByRecord, but I used the technique you describe extensively while building XML:Validator::Schema. Usually it's a simple matter of keeping a stack of data in your filter object. So when you see a category you do:

my $category_stack = $self->{category_stack} ||= []; push @$category_stack, { some => $category, data => $here };

Then when you see a title and you're ready to finish work on the category you just pop it off:

my $category_stack = $self->{category_stack}; my $category_data = pop @$category_stack;

Using a stack allows categories to nest. Popping off the category data means the stack is conservative and will shrink as stored data is used. You definitely want to avoid building a data structure that can contain all the category data in your file.

Does that make sense?

-sam

Replies are listed 'Best First'.
Re^2: Modifying Records with XML::SAX::ByRecord
by Lorphos (Novice) on Sep 03, 2007 at 08:55 UTC
    It sure makes sense to me, thank you! I'd have to use shift instead of pop to keep the elements in the order they appeared in.
    What I ended up doing is very similar to what you proposed:
    if ($collecting) { push @event_queue, sub { $self->SUPER::start_element($xml_data); }; }
    then, later on I empty the queue by executing the closures:
    foreach (@event_queue) { &$_(); }