Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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

by nasaa (Novice)
on Apr 28, 2014 at 16:21 UTC ( #1084146=note: print w/replies, xml ) Need Help??

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

hi thanks for replying.It works with smaller file.My machine only has 2 gig ram. I looked at XML::Twig but cant figure out what i need to change. i made following changes and then i am lost in the twig docs.
use XML::Twig; my $XML = XML::Twig->new($location.'CategoriesList.xml') or die("Unabl +e to parse CategoriesList.xml: $!");;

Replies are listed 'Best First'.
Re^3: Perl XML::Smart Out of memory! error
by poj (Abbot) on Apr 28, 2014 at 19:46 UTC
    #!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"); }
      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 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>


        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); }
        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
      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 + line 108. 1 1 DBD::mysql::st execute failed: Column 'category_description' cannot be + null at 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

      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.
Re^3: Perl XML::Smart Out of memory! error
by BrowserUk (Pope) on Apr 28, 2014 at 18:08 UTC
    then i am lost in the twig docs.

    Sorry, I can't help with that. I've never had occasion to use Twig.

    Perhaps you should start another thread asking for help doing the conversion.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2019-10-16 16:38 GMT
Find Nodes?
    Voting Booth?