From:   Ken Foskey <[EMAIL PROTECTED]>
> I am extracting addresses from an XML file to process through other
> programs using pipe delimiter the following code works but this is going
> to get 130,000 records through it it must be very efficient and I cannot
> follow the documentation on the best way to do this.
> 
> After this simple one is programmed I have to change a much more complex
> version of this program.
> 
> <snipped>
>
> my $ref = XML::Twig->new( twig_handlers=>{mem=>\&member} ) 
>     or die "Unable to open $ARGV[0] $!";
> 
> my $member = 0;
> 
> $ref->parsefile( $ARGV[0] );
> 
> sub get_value
> {
>     my ($mem_ref, $key) = @_;
>     my @array = $mem_ref->descendants( $key );
>     return $array[0]->text();
> }
> 
> sub member {
>     my ($t, $mem_ref) = @_;
>     $member++;
> 
>     my $mem_no = get_value( $mem_ref, 'member' );
>     my $add1   = get_value( $mem_ref, 'add1' );
>     my $add2   = get_value( $mem_ref, 'add2' );
>     my $add3   = get_value( $mem_ref, 'add3' );
>     my $suburb = get_value( $mem_ref, 'suburb' );
>     my $state  = get_value( $mem_ref, 'state' );
>     my $pcode  = get_value( $mem_ref, 'pcode' );
> 
>     print $sort join( '|', $member,
>                      $mem_no,
>                      no_pipe( $add1 ),
>                      no_pipe( $add2 ),
>                      no_pipe( $add3 ),
>                      no_pipe( $suburb),
>                      $state,
>                      $pcode,
>                     ) ."\n";
>     return 1;
> }

Looks like a perfect task for XML::Rules:

#!/usr/bin/perl
use strict;
use XML::Rules;

my $member;
my $parser = XML::Rules->new(
        stripspaces => 7,
        rules => {
                _default => 'content',
                mem => sub {
                        print join( '|', ++$member, map {(my $s = $_[1]->{$_}) 
=~ s/\|//; 
$s} qw(member add1 add2 add3 suburb state pcode)), "\n";
                        return;
                }
        },
);

$parser->parse(\*DATA);

__DATA__
<root>
   <mem>
     <member>member</member>
     <add1>add1</add1>
     <add2>add2</add2>
     <add3>add3</add3>
     <suburb>suburb</suburb>
     <state>state</state>
     <pcode>pcode</pcode>
   </mem>
   <mem>
     <member>other</member>
     <add1>ADD1</add1>
     <add2>ADD2</add2>
     <add3>ADD3</add3>
     <suburb>suburb 2</suburb>
     <state>state</state>
     <pcode>pcode</pcode>
   </mem>
</root>



I would expect this to be quicker than your XML::Twig solution, 
though I have to leave the benchmarking to you.

HTH, Jenda
===== [EMAIL PROTECTED] === 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to