- 论坛徽章:
- 0
|
本帖最后由 litdong 于 2010-07-21 17:59 编辑
刚学perl,google了些网上的sample code组合了一下,觉得很乱,帮忙看看如何改进,大致是这样,数据库被导出成xml格式,需要perl解析xml并写到数据库中

- #!/usr/bin/perl -w
- use strict;
- use XML::LibXML;
- use XML::LibXML::Common;
- use DBI;
- # 处理参数传入,如果没有参数,则显示提示帮助信息给用户
- if ( @ARGV < 1 ) {
- print STDERR "\n == INTRODUCTION ==\n";
- print STDERR "\n == PARAMETERS ==\n";
- print STDERR " -f <mandatory input> xml file you want to load\n";
- print STDERR " -h <mandatory input> database host name \n";
- print STDERR " -u <mandatory input> database user name \n";
- print STDERR " -p <optional input> database password,default is empty \n";
- print STDERR " -d <mandatory input> database name \n";
- print STDERR "\n == EXAMPLE ==\n";
- print STDERR "$0 -f /usr/demo.xml -h 135.252.234.35 -u mysql -p -d fmbb \n";
- exit;
- }
- my ( $temp, $xmlfile, $hostname, $username, $password, $databasename, );
- # 密码缺省可设置为空
- $password = "";
- #遍历参数,赋予相应变量
- while (@ARGV) {
- $temp = shift;
- if ( $temp eq "-f" ) { $xmlfile = shift; }
- elsif ( $temp eq "-h" ) { $hostname = shift; }
- elsif ( $temp eq "-u" ) { $username = shift; }
- elsif ( $temp eq "-p" ) { $password = shift; }
- elsif ( $temp eq "-d" ) { $databasename = shift; }
- else {
- print STDERR "Warning: argument $temp igorned.\n";
- }
- }
- #检查各个传入的参数值是否为空
- if ( !defined($xmlfile) ) {
- print STDERR "Error: argument '-f' is needed.\n";
- exit;
- }
- if ( !defined($hostname) ) {
- print STDERR "Error: argument '-h' is needed.\n";
- exit;
- }
- if ( !defined($databasename) ) {
- print STDERR "Error: argument '-d' is needed.\n";
- exit;
- }
- if ( !defined($username) ) {
- print STDERR "Error: argument '-u' is needed.\n";
- exit;
- }
- #根据传入参数,拼接连接数据库的字符串
- my $connectionstr = "DBI:mysql:" . $databasename . ";host=" . $hostname;
- print "Conect to database " . $hostname . " " . "$databasename" . "... \n";
- #连接数据库
- my $dbh =
- DBI->connect( $connectionstr, $username, $password,
- { RaiseError => 1, AutoCommit => 0 } )
- || die "Could not connect to database: $DBI::errstr";
- print "Start to parse source file ... \n";
- #载入xml
- my $parser = XML::LibXML->new();
- my $doc = $parser->parse_file($xmlfile);
- print "Parse complete, ready to dump into database \n";
- #定义xml读出结果的变量
- my ( $a, $b, $c, $d, $e, $f, $g );
- #定义执行语句的结果
- my $success = 1;
- #设置一个计数器
- my $count = 0;
- #开始遍历xml,把读出的值放在标量中,并打印导出内容到屏幕上
- my $root = $doc->getDocumentElement();
- my @acknowledgedAlarms_nodes = $root->childNodes;
- foreach my $acknowledgedAlarm (@acknowledgedAlarms_nodes) {
- print "############\n";
- if ( $acknowledgedAlarm->nodeType == ELEMENT_NODE ) {
- foreach my $values ( $acknowledgedAlarm->childNodes ) {
- if ( $values->nodeType != XML_TEXT_NODE ) {
- if ( $values->nodeName() eq "a" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $a = $values->textContent;
- }
- elsif ( $values->nodeName() eq "b" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $b = $values->textContent;
- }
- elsif ( $values->nodeName() eq "c" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $c = $values->textContent;
- }
- elsif ( $values->nodeName() eq "d" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $d = $values->textContent;
- }
- elsif ( $values->nodeName() eq "e" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $e = $values->textContent;
- }
- elsif ( $values->nodeName() eq "f" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $f = $values->textContent;
- }
- elsif ( $values->nodeName() eq "g" ) {
- print $values->nodeName() . ": "
- . $values->textContent . "\n";
- $g = $values->textContent;
- }
- else {
- print $values->nodeName() . ": " . $values->textContent;
- print "\n";
- }
- }
- }
- #计数器加1
- $count++;
- print "############\n";
- #执行,当然最后commit才会放到数据库中
- my $insert_handle =
- $dbh->prepare_cached('INSERT INTO ack_alarms VALUES (?,?,?,?,?,?,?)');
- $success &&=
- $insert_handle->execute( $a, $b, $c,
- $d, $e, $f, $g );
- }
- }
- #commit到数据库中,有问题则回滚
- my $result = ( $success ? $dbh->commit : $dbh->rollback );
- #写库失败的话终止
- unless ($result) {
- $count=0;
- $dbh->disconnect();
- die "Couldn't finish transaction: " . $dbh->errstr;
- }
- #断开数据库链接
- $dbh->disconnect();
- print "Totally import " . $count . " rows to database \n";
复制代码 |
|