Hallo boys.
I'm writing now some code for download XML file, parse it and put into MySQL
database but result is other then expected :-(
XML file is encoded in windows-1250 codepage, database too but after done in
database are stored bad coded every rows where are other then US-ASCII
characters. IMHO XML::Parser switch something in Perl into Unicode mode and
I'm unable to switch it back to non-translating mode.
If I try to debug my code in Komodo 3.5 debuger all work until first bad
encoded string go into Mysql.pm module. Commonly I can to trace Mysql.pm
line by line, but if this module get bad coded string as parameter Komodo
debuger crash.
Any idea ?
My code:
#!/usr/bin/perl
use strict;
use DBI;
use LWP::UserAgent;
use XML::Parser;
use Unicode::Lite;
&readini; # get variables for next line
our $dbh = DBI->connect("DBI:mysql:$mydb:$host",$db_user,$db_password)
or die "Can't connect: $DBI::errstr\n";
my $ua = LWP::UserAgent->new(timeout => 600);
# some code for initialize connection
my $c=$res->content; # geting document from web
my $pars=new XML::Parser(Handlers => {
Start => \&handle_start,
Char => \&handle_text});
$pars->parse($c,list=>'',item=>'',values=>[]);
$rc=$dbh->disconnect;
sub handle_start {
my $self=shift;
my @arg=shift;
my $item=$self->{Context}->[$#{$self->{Context}}];
if($item eq 'body' and $arg[0] eq 'list_prs') {
$self->{list}=$arg[0];}
elsif($item eq 'item') {
$self->{item}=$arg[0];}
}
sub handle_text {
my $self=shift;
my @arg=shift;
if($self->{list} eq 'list_prs') {
$arg[0]='' if(ord($arg[0])==10);
if($self->{item} eq 'prs_id') {
$self->{values}[0]=$arg[0];}
elsif($self->{item} eq 'prs_name') {
$self->{values}[1]=$arg[0];
&to_table($self);
$self->{values}=[] if($arg[0] eq 'item');}
$self->{item}='';}
}
sub to_table {
my $self=shift;
my $prs_id=$self->{values}[0];
my $prs_name=convert( 'utf8', 'cp1250', $self->{values}[1]);
$prs_name=quotemeta($prs_name);
########################################
# here debuger crash on bad encoded $prs_name
# or $prs_name is stored into table badly
$sth=$dbh->prepare("insert into list_prs set prs_id=\'$prs_id\',
prs_name=\'$prs_name\'");
########################################
$sth->execute() or die $sth->errstr;
$sth->finish;
}
Petr Vileta, Czech republic
(My server reject all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs