Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^3: Perl XML::Smart Out of memory! error

by poj (Abbot)
on Apr 28, 2014 at 19:46 UTC ( [id://1084188]=note: print w/replies, xml ) Need Help??


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

#!perl use strict; use DBI; use XML::Twig; my $dbh = dbh(); $dbh->do("DROP TABLE IF EXISTS ice_categories"); $dbh->do("CREATE TABLE ice_categories ( category_id int(11) not null, parent_cat_id int(11) not null, category_name varchar(100) not null default '', category_description varchar(100) not null default '', category_image varchar(100) not null default '', category_thumb varchar(100) not null default '', KEY (category_id), KEY (parent_cat_id)) CHARACTER SET utf8 COLLATE utf8_unicode_ci;"); my $sql = 'INSERT INTO ice_categories ( category_id, parent_cat_id, category_name, category_description, category_image, category_thumb) VALUES (?,?,?,?,?,?)'; my $sth = $dbh->prepare($sql); my $t = XML::Twig->new( twig_handlers => { 'Category' => \&Category } ); $t->parsefile( 'file.xml' ); 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'); $f[2] = $elt->first_child('Name[@langid="1"]')->att('Value'); $f[3] = $elt->first_child('Description[@langid="1"]')->att('Value'); print "@f\n"; $sth->execute(@f); } # connect sub dbh { my $dsn = "DBI:mysql:database=test;host=localhost"; my $dbh = DBI->connect($dsn, 'user', 'password', {RaiseError => 1, PrintError => 1}) or die (Error connecting " $DBI::errstr"); }
poj

Replies are listed 'Best First'.
Re^4: Perl XML::Smart Out of memory! error
by nasaa (Novice) on Apr 28, 2014 at 22:53 UTC
    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>

      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
      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
Re^4: Perl XML::Smart Out of memory! error
by nasaa (Novice) on Apr 28, 2014 at 23:29 UTC
    i tried
    $f[3] = eval { $elt->first_child('Description[@langid="1"]')->att('Val +ue'); };
    i get an error
    Use of uninitialized value $f[3] in join or string at import_icecat.pl + line 108. 1 1 DBD::mysql::st execute failed: Column 'category_description' cannot be + null at import_icecat.pl line 109.

      i tried ... i get an error

      Great, so what is your question?

      Q: Hello Mr.Bartender, I don't have my langid, can I have a drink?

      A: I need to see langid

Re^4: Perl XML::Smart Out of memory! error
by nasaa (Novice) on Apr 29, 2014 at 00:20 UTC
    lol.. its 2 am here.. your joke cracked me up..:) thank you.. my question is how do i get the script to ignore where the tags are not present.been looking at the xml and in cases the discription or another tag is not present.I want the script to carry on and ignore mising tags..
      opps.. shoudl have been bit clear in previous post. if tag is missing then $f[3] = eval { $elt->first_child('Description[@langid="1"]')->att('Value'); }; Works.But if tags are not closed properly like
      <Category ID="1" LowPic="" Score="0" Searchable="0" ThumbPic="" UNCA +TID="" Visible="0"> <Name ID="0" Value="" langid="1"/> <ParentCategory ID="1"/> </Category>
      then i get an error..If i delete this .then it works.Otherwose it throws an error as mentioned earlier.
        Well, you could also eval { $sth->execute(@f); }; </c> that way if @f is missing stuff that just has to be there, like langid, you can ignore the raised error (eval catches it)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1084188]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-24 18:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found