Imagine you have a huge XML. It has some header, then a <Foos> tag full of <Foo> tags and nothing more. And you have a tool that's able to somehow process such XML if only it either contained just one <Foo> or at least was not so big. Here is a tiny script that allows you to specify the name of the <Foo> tag, the huge file and floods your disk with little files containing one <Foo> each:
use strict; use warnings; no warnings 'uninitialized'; use XML::Rules; die "Usage: $0 split_tag filename(s)\n" unless @ARGV >= 2; my ($split_tag, @files) = @ARGV; my $parser = XML::Rules->new( rules => [ _default => 'raw', $split_tag => sub { my ($file, $id) = ( $_[4]->{parameters}{'file'}, ++$_[4]-> +{parameters}{'id'}); $id = sprintf "%04d", $id; $file =~ s/(?:\.xml)?$/-$id.xml/i; if (ref $_[3]->[-1]{_content}) { $_[3]->[-1]{_content}[-1] =~ s/^.*(\n[^\n]+)$/$1/s; } else { $_[3]->[-1]{_content} =~ s/^.*(\n[^\n]+)$/$1/s; } print " $file\n"; open my $FH, '>:utf8', $file or die qq{Can't create "$file +": $^E\n}; print $FH $_[4]->parentsToXML(); print $FH $_[4]->ToXML( $_[0], $_[1]),"\n"; print $FH $_[4]->closeParentsToXML(); close $FH; return; } ] ); foreach my $file (@files) { $parser->parsefile( $file, {file => $file}); }
That's it folks ;-)
And you only ever have the header plus one <Foo> in memory. Of course if you wanted to get rid of the <Foos>, rename the <Foo> or something, you can add that.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Split XML to chunks
by mr_mischief (Monsignor) on Aug 23, 2007 at 19:04 UTC |