in reply to Re: XML::Twig to mysql totally lost
in thread XML::Twig to mysql totally lost

Thanks for the code but this is what I have so far

# !/usr/local/bin/perl -w BEGIN { my $base_module_dir = (-d '/home/bcnagle/perl' ? '/home/bcnagle/pe +rl' : ( getpwuid($>) )[7] . '/perl/'); unshift @INC, map { $base_module_dir . $_ } @INC; } use strict; use XML::Twig; use DBI; my $path; my $Product_ID; my $Updated; my $Quality; my $Supplier_id; my $Prod_ID; my $Catid; my $On_Market; my $Model_Name; my $Product_View; my $HighPic; my $HighPicSize; my $HighPicWidth; my $HighPicHeight; my $Date_Added; my $dbh= connect_to_db(); my $insert= $dbh->prepare( "INSERT INTO files (path, Product_ID, Upda +ted, Quality, Supplier_id, Prod_ID, Catid, On_Market, Model_Name, Pro +duct_View, HighPic, HighPicSize, HighPicWidth, HighPicHeight, Date_Ad +ded) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"); my $twig = new XML::Twig( twig_handlers => {indexfile => \$file} ); $twig->parsefile( "/file.xml" ); $twig->flush; # flush the end of t +he twig $dbh->disconnect(); exit; sub connect_to_db { my $driver = "mysql"; my $dsn = "DBI:$driver:database=database;"; my $dbh = DBI->connect($dsn, 'uname', 'pword', {AutoCommit=>1}); my $drh = DBI->install_driver($driver); return( $dbh); } sub indexfile { my($twig, $file) = @_; $path = $file->att( 'path'); $Product_ID = $file->att( 'Product_ID'); $Updated = $file->att( 'Updated'); $Quality = $file->att( 'Quality'); $Supplier_id = $file->att( 'Supplier_id'); $Prod_ID = $file->att( 'Prod_ID'); $Catid = $file->att( 'Catid'); $On_Market = $file->att( 'On_Market'); $Model_Name = $file->att( 'Model_Name'); $Product_View = $file->att( 'Product_View'); $HighPic = $file->att( 'HighPic'); $HighPicSize = $file->att( 'HighPicSize'); $HighPicWidth = $file->att( 'HighPicWidth'); $HighPicHeight = $file->att( 'HighPicHeight'); $Date_Added = $file->att( 'Date_Added'); $insert->bind_param( 1, $path); $insert->bind_param( 2, $Product_ID); $insert->bind_param( 3, $Updated); $insert->bind_param( 4, $Quality); $insert->bind_param( 5, $Supplier_id); $insert->bind_param( 6, $Prod_ID); $insert->bind_param( 7, $Catid); $insert->bind_param( 8, $On_Market); $insert->bind_param( 9, $Model_Name); $insert->bind_param( 10, $Product_View); $insert->bind_param( 11, $HighPic); $insert->bind_param( 12, $HighPicSize); $insert->bind_param( 13, $HighPicWidth); $insert->bind_param( 14, $HighPicHeight); $insert->bind_param( 15, $Date_Added); $insert->execute(); $twig->purge; }

I am getting a shell error when running the script stating that

weaken is only available with the XS version of Scalar::Util at /home/bcnagle/perl/usr/lib/perl5/site_perl/5.8.8/XML/Twig.pm line 117 BEGIN failed--compilation aborted at /home/bcnagle/perl/usr/lib/perl5/site_perl/5.8.8/XML/Twig.pm line 172. Compilation failed in require at /home/bcnagle/public_html/xmlparse.pl line 9. BEGIN failed--compilation aborted at /home/bcnagle/public_html/xmlparse.pl line 9.

Replies are listed 'Best First'.
Re^3: XML::Twig to mysql totally lost
by mirod (Canon) on Jul 15, 2011 at 07:48 UTC
    You are probably using a RedHat-based distribution, which includes Scalar::Util without its XS part, and so doesn't provide weaken (see Task::Weaken). The solution would be to re-install Scalar::Util, from CPAN.

    Also, I am not sure why you do a $twig->flush; at the end of your code, since you are not outputting the XML at all.

      I am very new to this, from what the tutorial showed, i added it in, i didn't think i needed it either since it's a cron and not outputting but want to keep memory to a minimum consumption. Should i remove it? From what the code shows, is it for certain looping then clearing the previous parse after uploading to the database? want to make sure I'm on the right path.

        The purge in your handler releases the memory by deleting everything but the direct ancestors of the current element. The flush is used when you want to output the transformed XML, by using flush in handlers. Then you need to flush what remains in memory after you're done parsing. At least with old versions of the module, recent versions take care of this automatically.

        So the short answer is: yes you can delete it.