[32279] in Perl-Users-Digest
Perl-Users Digest, Issue: 3546 Volume: 11
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Thu Nov 17 09:09:20 2011
Date: Thu, 17 Nov 2011 06:09:04 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Thu, 17 Nov 2011 Volume: 11 Number: 3546
Today's topics:
Re: How to import only part of a large XML file? <klaus03@gmail.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Wed, 16 Nov 2011 11:53:37 -0800 (PST)
From: Klaus <klaus03@gmail.com>
Subject: Re: How to import only part of a large XML file?
Message-Id: <5d89d42e-360f-4965-8c47-df024dc7edea@m7g2000vbc.googlegroups.com>
On 16 nov, 17:32, ccc31807 <carte...@gmail.com> wrote:
> On Nov 11, 5:39=A0pm, Dwight Army of Champions
>
> <dwightarmyofchampi...@hotmail.com> wrote:
> > I have a very large XML file that I want to load, but I don't want to
> > necessarily load the entire document; that takes too long. What I want
> > to do instead is only key/value pairs that meet certain criteria, like
> > only grab entries whose value fall within a certain date for a key
> > date_of_entry. Can I just use XML::Simple for this or do I need a
> > better module?
>
> This depends on the nature of your input. I do this kind of thing
> every day, and use a simple regular expression to filter the file. Of
> course, you still have to read every line of the file to make sure
> that you catch all of your intended targets, but you would have to do
> that anyway. This is the kind of task for which it's a lot easier to
> hand roll your own parser than it is to look for, evaluate, learn,
> install, and use some third party module. In my opinion anyway. For
> example:
>
> SCRIPT
> #! perl
> use warnings;
> use strict;
> my %filter;
> while (<DATA>)
> {
> =A0 =A0 next unless /\w/;
> =A0 =A0 chomp;
> =A0 =A0 if ($_ =3D~ m!<order>(\d+)</order>!)
> =A0 =A0 {
> =A0 =A0 =A0 =A0 my $key =3D $1;
> =A0 =A0 =A0 =A0 while (<DATA>)
> =A0 =A0 =A0 =A0 {
> =A0 =A0 =A0 =A0 =A0 =A0 last if $_ =3D~ m!</pres>!;
> =A0 =A0 =A0 =A0 =A0 =A0 next unless $_ =3D~ m!<last>(\w+)</last>!;
> =A0 =A0 =A0 =A0 =A0 =A0 $filter{$key} =3D $1;
> =A0 =A0 =A0 =A0 }
> =A0 =A0 }}
>
> print "Finished processing file\n";
> foreach my $key (sort keys %filter) { print "$key =3D> $filter{$key}
> \n"; }
> exit(0);
Using XML::Reader, it's even easier:
use strict;
use warnings;
use XML::Reader;
my %filter;
my $rdr =3D XML::Reader->new(\*DATA,
{mode =3D> 'branches'},
{ root =3D> '/data/pres', branch =3D> [
'/order',
'/last',
]});
while ($rdr->iterate) {
my ($order, $last) =3D $rdr->value;
$filter{$order} =3D $last;
}
print "Finished processing file\n";
foreach my $key (sort keys %filter) {
print "$key =3D> $filter{$key}\n";
}
__DATA__
<data>
<pres>
<order>1</order>
<first>George</first>
<last>Washington</last>
<year>1788</year>
</pres>
<pres>
<order>2</order>
<first>John</first>
<last>Adams</last>
<year>1796</year>
</pres>
<pres>
<order>3</order>
<first>Thomas</first>
<last>Jefferson</last>
<year>1800</year>
</pres>
</data>
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V11 Issue 3546
***************************************