Re: Parsing w/XML::Twig & Perl
by Mike Blezien other posts by this author
Jul 23 2007 6:28AM messages near this date
view in the new Beta List Site
RE: Parsing w/XML::Twig & Perl
|
New to list
& XSLT Cahoon,
that was the problem. thanks for the suggestion.
Mike
----- Original Message -----
From: "Cahoon, Forrest" <Forrest.Cahoon@[...].com>
To: "Mike Blezien" <mickalo@[...].net> ; "Perl-XML List"
<perl-xml@[...].com>
Sent: Monday, July 23, 2007 8:03 AM
Subject: RE: Parsing w/XML::Twig & Perl
The error message is telling you that $tracks is sometimes uninitialized, which
would happen when there's a <product> node that doesn't have <tracks> inside of
it.
You obviously expect that to happen sometimes because your very next line is
"return unless $tracks;"
to avoid iterating through non-existent tracks.
Move that return statement above your problem line and your problem will be
fixed.
Forrest Cahoon
not speaking for merrill corporation
> -----Original Message-----
> From: perl-xml-bounces@[...].com
> [mailto:perl-xml-bounces@[...].com] On Behalf
> Of Mike Blezien
> Sent: Saturday, July 21, 2007 6:15 AM
> To: Perl-XML List
> Subject: Parsing w/XML::Twig & Perl
>
> Hello,
>
> we've run a few test and everything seems to be working as
> expected, but got one little problem I haven't been able to
> figure out, why we keep getting this error (code snipt below)
> ----
> Can't call method "first_child_text" on an undefined value at
> .. /sample.cgi line 56 which is this line "my $tracknums =
> $tracks->first_child_text('number_of_tracks');
> ----
> a value for the "$tracknums" is returned and all other values
> are returned as expected after it parses the XML file.
> Haven't been able to figure out why I keep getting this error??
>
> ##############################################################
> ##############
> my $twig = new XML::Twig(twig_handlers => { product =>
> \&get_products });
> $twig->parsefile("$xmlfile"); $twig->purge();
> ##############################################################
> ##############
> sub get_products {
> my($t,$elt) = @_;
> my($track_title,$trackno,$setno,$soundtype,$codec,$file);
>
> # process each product loop.
> my $article_number = $elt->first_child_text('article_number');
> my $dist_number = $elt->first_child_text('distributor_number');
> my $dist_name = $elt->first_child_text('distributor_name');
> my $artist = $elt->first_child_text('artist');
> my $ean_upc = $elt->first_child_text('ean_upc');
> my $set_total = $elt->first_child_text('set_total');
>
> my $tracks = $elt->first_child('tracks');
>
> # LINE 56 here
> my $tracknums = $tracks->first_child_text('number_of_tracks');
>
> return unless $tracks;
>
> for my $track ($tracks->children('track'))
> {
> $track_title = $track->first_child_text('title');
> $trackno = $track->first_child_text('trackno');
> $setno = $track->first_child_text('setno');
>
> for my $sound ($track->children('sound'))
> {
> $soundtype = $sound->first_child_text('sound_type');
> $codec = $sound->first_child_text('codec');
> $file = $sound->first_child_text('file');~;
> }
>
> } # close for $track loop
> # free up memory
> $t->purge();
> }
_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Mike Blezien
Forrest Cahoon
Mike Blezien
|