Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Best way to Download and Process a XML file

by BrowserUk (Pope)
on Sep 25, 2012 at 15:02 UTC ( #995564=note: print w/replies, xml ) Need Help??

in reply to Best way to Download and Process a XML file

The xml feed can be quite huge, (~150Gb max.)

Often as not with XML files that big, the feed consists of one top level tag that contains a raft of much smaller, identical (except an ID) substructures:

<top> <sub name=1> ... </sub> <sub name=2> ... </sub> ... </top>

It therefore becomes quite simple to do a preliminary parse of the datastream and break the huge dataset down into manageable chunks for processing:

#! perl -slw use strict; use Data::Dump qw[ pp ]; use XML::Simple; # open DATA, '<', 'stream'; my $enc = <DATA>; my $bot = my $top = <DATA>; $bot =~ s[^<(\w+).*][</$1>]s; my $section = ''; until( ( my $line = <DATA> ) =~ m[$bot] ) { my( $tag ) = $line =~ m[<(\w+)]; my $end = "</$tag>"; $section .= $line; $section .= <DATA> until $section =~ m[$end\s*$]; my $ref = XMLin( $enc . $top . $section . $bot ); ## do something with this section pp $ref; $section = ''; } __DATA__ <?xml version="1.0" encoding="UTF-8"?> <top> <sub name='1'> <subsub> some stuff </subsub> </sub> <sub name='2'> <subsub> some stuff </subsub> </sub> <sub name='3'> <subsub> some stuff </subsub> </sub> <sub name='4'> <subsub> some stuff </subsub> </sub> <sub name='5'> <subsub> some stuff </subsub> </sub> </top>


C:\test>\perl64-10\bin\perl { "sub" => { name => 1, subsub => "\n some stuff\n " } +, } { "sub" => { name => 2, subsub => "\n some stuff\n " } +, } { "sub" => { name => 3, subsub => "\n some stuff\n " } +, } { "sub" => { name => 4, subsub => "\n some stuff\n " } +, } { "sub" => { name => 5, subsub => "\n some stuff\n " } +, }

Of course, this 'breaks the rules' of XML processing, and requires you to assume some knowledge of the details of the XML you will be processing. But then the kinds of details required are usually, a) easily discovered; b) rarely change; c) easily catered for when and if they do change.

So if you favour the pragmatism of getting the job done over more esoteric -- and revenue sink -- criteria such as 'being correct', bending the rules a little can save you a lot of time, effort and expense.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

RIP Neil Armstrong

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://995564]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2018-06-18 08:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.