Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have a 500mb XML file, and I need to know how to parase it. here is an example:
<offer id="11"> <f1>Data1</f1> <f2>data2</f2> </offer> <offer id="13"> <f1>Data1</f1> <f2>data2</f2> </offer>

I just need to be able to do a print $id,$f1,$f2 to put into a rdbms.
Can anyone show me how to go about this, I am doing it now in PHP and php just does not cut it anymore, so I have come over to perl :)

Replies are listed 'Best First'.
Re: example for parseing xml with xml::twig
by Tanktalus (Canon) on Sep 10, 2005 at 03:45 UTC

    With small files (say, < 100MB) , I usually just load the whole thing in, and then get all the elements I'm interested in, and work on them - I find it easier than the stream interface. However, in your case, we might be stretching it unless you've got a 64-bit perl. So here's a stream example. Note that for your example, I just put the whole thing in "<something>..</something>" tags so it'd be valid XML.

    use strict; use warnings; use XML::Twig; my $twig = XML::Twig->new( twig_handlers => { offer => sub { printf("%s,%s,%s\n", $_->att('id'), $_->first_child('f1')->text() +, $_->first_child('f2')->text() +, ); } } ); $twig->parsefile('a.xml');

    Hope that helps

Re: example for parseing xml with xml::twig
by Anonymous Monk on Sep 10, 2005 at 03:02 UTC

    Why did you post this when you already have a thread on the same topic? Besides, show some effort, read document, try it. Come here again, if you runinto trouble.

    You have no respect to other people's effort.

Re: example for parseing xml with xml::twig
by Anonymous Monk on Sep 10, 2005 at 03:06 UTC

    Some of the posts use Katrina's name are not real, and even it is real, lots of those projects are just frauds. I don't want to be used in the wrong way.

      I am sorry, if that is not the way you do things here. I am new at this please give me a break.
Re: example for parseing xml with xml::twig
by planetscape (Chancellor) on Sep 11, 2005 at 05:10 UTC

    Whether or not the invocation of Katrina is real or designed to solicit free help doesn't matter much, IMHO. Take a look at some of the real suggestions offered here for help on the question of parsing large XML files. :-)

    planetscape