From: saw <saweinfi...@gmail.com>

> Given one large XML file such as:
> <A>
>    <B>
>       <C><D><E E="Eattr"/><F F="Fattr"/><G/></D></C>
>       <C><D><E E="Eattr"/><F F="Fattr"/><G/></D></C>
> <!-- many more <C> sub-trees  -->
>    </B>
> </A>
> 
> I want to create many small XML files consisting of a Root element and
> the <C> sub-tree. I would like to copy the <C> sub-tree from input to
> output as a block without having to address the sub-components. I have
> read the input into a hash using XML::Simple, but don't know how to
> copy the sub-tree which is also a hash.

#!perl
use strict;
use warnings; no warnings 'uninitialized';

use XML::Rules;

my $counter = 1;
my $parser = XML::Rules->new(
        rules => {
                _default => 'raw',
                C => sub {
                          my ($tag,$attrs,$parser) = @_[0,1,4];
                          my $file = sprintf "results%06d.xml", $counter++;

                          open my $OUT, '>', $file or die "Cannot create $file! 
$^E\n";
                          print $OUT $parser->ToXML($tag,$attrs);
                          close $OUT;
                          return;
                }
        }
);

$parser->parse(\*DATA);

__DATA__
<A>
   <B>
      <C><D><E E="Eattr"/><F F="Fattr"/><G/></D></C>
      <C><D><E E="Eattr222"/><F F="Fattr"/><G/></D></C>
<!-- many more <C> sub-trees  -->
   </B>
</A>




At most the contents of one <C> tag will be in the memory at a time.

Jenda
===== je...@krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to