Mike Blezien wrote:

Rob Dixon wrote:

Mike Blezien wrote:

we need to parse some very large XML files, approx., 900-1000KB's filesize.

[snip]

Your application of XML::Twig seems exactly right. I'm not sure
what it is you don't understand, but if you use this as your
'get_products' subroutine I hope it answers some questions. All it
does is print the title of the product and the title of all the
tracks in that product. Post again if you have any trouble understanding what I've written.

 sub get_products {

   my $product = $_;

   my $product_title = $product->first_child('title');
   print $product_title->trimmed_text, "\n";

   my $tracks = $product->first_child('tracks');
   return unless $tracks;

   foreach my $track ($tracks->children('track')) {
     my $track_title = $track->first_child('title');
     print '  ', $track_title->trimmed_text, "\n";
   }

   print "\n";
 }

HTH,

Rob

Ok, this helps getting me in the right direction, much appreciate the help.

The only question I have now, is while looping through the <track> </track> we have another loop inside each for the <track>
.....
.....
  <sound>
    <file> ... </file>
    <sound_type> ... </sound_type>
    <codec> ... </codec>
    <bitrate> ... </bitrate>
    <channels>mono</channels>
</sound>
......
.......
</track>
can one do something like this:

foreach my $track ($tracks->children('track'))
{
 for my $sound ($track->first_child('sound'))
   {
      my $soundtype =  $sound->first_child_text('sound_type');
      my $codec       =  $sound->first_child_text('codec');
   }
  my $track_title = $track->first_child('title');
 print '  ', $track_title->trimmed_text, "\n";
}

Would this work or is there a better way to do this ?

Almost right. You need

 for my $sound ($track->children('sound')) {
   :
 }

(and you also need to test it!)

One thing to be careful of is that all the variables in my code were XML
nodes which could be both used to locate child nodes and to extract their
text values. You have variables which contain just the text values such as

 my $codec = $sound->first_child_text('codec');

which is ok as long as you understand the difference and can keep track
of which is which. You may want to stick with variables being XML nodes
throughout, such as:

 my $codec = $sound->first_child('codec');
 print $codec->trimmed_text, "\n";

HTH,

Rob


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to