in reply to Getting into a SAX frame of mind

XML::Twig is especially designed to work with big files. What you want to do can be done with the following code:

#!/usr/bin/perl -w use strict; use XML::Twig; my $t= XML::Twig->new( twig_handlers => { node => sub { my( $t, $node)= @_; return unless( $node->has_children( 'child')); my $name= $node->first_child( 'name'); my $file= $name->text; $name->delete; print "in file $file\n"; # in real code open f +ile $node->print; # in real code print +to file print "\n\n"; $t->purge; # get rid of the node } })->parse( \*DATA); __DATA__ <root> <name>Parent Name</name> <node><name>Node 1</name> <node><name>Node 2</name> <child id="2263583"> <start> <thing value="20001231945"/> </start> </child> </node></node> <node><name>Node 3</name> <node><name>Node 4</name> <child id="2274693"> <start> <thing value="20001231930"/> </start> </child> </node> </node> </root>

Replies are listed 'Best First'.
Re: Re: Getting into a SAX frame of mind
by qq (Hermit) on Jan 23, 2004 at 22:39 UTC

    You missed the requirement of having the file name derived from the names of ancestors. You'd need to add someting like:

    $file = join '_', map { $_->first_child_text('name') } $node->ancestor +s( 'node' ); # still need to add name from root/name

    (Which I know you know, but for the OP's benefit.)

    qq, a Twig fan

      Oops! I missed the 's' at the end of 'elements' Thanks.

      I forgot to mention BTW that the nice thing about the above code is that at most you keep one node in memory.