http://www.perlmonks.org?node_id=1084206


in reply to Re^3: Perl XML::Smart Out of memory! error
in thread Perl XML::Smart Out of memory! error

Hi many thanks for the code. i ran it on the full file and it showed up an error while processing. Can't call method "att" on an undefined value at import_icecat.pl line 107. line 107 is
$f[3] = $elt->first_child('Description[@langid="1"]')->att('Value');
looking at the xml i find that there is block with missing attributes.It has the opening and closing Category tag but not discription.How do i get it to ignore where tags are missing and keep processing
<Category ID="1" LowPic="" Score="0" Searchable="0" ThumbPic="" UNCA +TID="" Visible="0"> <Name ID="0" Value="" langid="1"/> <ParentCategory ID="1"/> </Category>

Replies are listed 'Best First'.
Re^5: Perl XML::Smart Out of memory! error
by poj (Abbot) on Apr 29, 2014 at 03:55 UTC

    Try

    sub Category{ my ($t, $elt) = @_; my @f = ( $elt->att('ID') ,'','','','','' ); $f[4] = $elt->att('LowPic'); $f[5] = $elt->att('ThumbPic'); $f[1] = $elt->first_child('ParentCategory')->att('ID'); for my $e ($elt->children('@langid="1"')){ $f[2] = $e->att('Value') if $e->name eq 'Name'; $f[3] = $e->att('Value') if $e->name eq 'Description'; } print "@f\n"; $sth->execute(@f); }
    poj
Re^5: Perl XML::Smart Out of memory! error
by Anonymous Monk on Apr 28, 2014 at 18:59 UTC
    block eval, like  $f[3] = eval { $elt->first_child('Description[@langid="1"]')->att('Value'); }; so if there is no first_child ... and no att value ... the program keeps going