Perl分析xml文件并生成csv数据文件,同时写入数据库

 

 

#!/usr/bin/perl

use XML::Simple;
use Data::Dumper;
use DBI;
use Encode;
use strict;
use encoding "gbk";

#判断是否传递XML文件参数
if (open(MYFILE, "$ARGV[0]")) {
    # here's what to do if the file opened successfully
    print "\n\"$ARGV[0]\" : file exist! parse beginning!\n";
    #获得EMS名称
    my @ems = split(/\_/,$ARGV[0]);
    print "@ems[1]\n";
   
    #获得XML文件数据
    my $data = XMLin($ARGV[0]);
    #print Dumper($data);

    #打印文件时间戳
    print "Timestamp = $data->{'timestamp'}\n\n";

    my $i = 0;
    #判断是否存在下一条网元记录
    print "appending to csv...\n";
    while( $data->{'output'}->{'TopoMgr.NEAdditionalInfoType-array'}->{'TopoMgr.NEAdditionalInfoType'}->[$i] )   
    {
        print "\@ record num = $i\n";
       
        my $text = "$data->{'output'}->{'TopoMgr.NEAdditionalInfoType-array'}->{'TopoMgr.NEAdditionalInfoType'}->[$i]->{'neName'}\n";
       
        print $text;
       
        #将分析出的数据追加到FILE末尾
        open(APPENDFILE, ">>perlne.dat");

        #转为utf-8后不报字符串过长异常,不转不影响效果
        Encode::_utf8_off($text);
        print APPENDFILE ($text);
        close(APPENDFILE);
       
        # Connect to target DB
        #my $dbh=DBI->connect("DBI:mysql:test:192.168.1.14", 'root', 'root');
        my $dbh = DBI->connect( 'DBI:Oracle:ora10g','res_col','res_col');

        #$dbh->do("SET character_set_client = 'utf-8'");
        #$dbh->do("SET character_set_connection = 'utf-8'");
        #$dbh->do("SET character_set_results= 'utf-8'");
        #$dbh->do("SET names  'gbk'");
        #Encode::_utf8_off($nename);

        # Insert one row
        my $rows = $dbh->do("INSERT INTO neinfo(text) VALUES ('$text')");
       
        $dbh->disconnect();
       
        $i ++;
    }
   
    close(MYFILE);
    # query
    my $dbh = DBI->connect( 'DBI:Oracle:ora10g','res_col','res_col');
    my $sqr = $dbh->prepare("SELECT * FROM neinfo");
    $sqr->execute();

    while ( my @row=$sqr->fetchrow_array() )
    {
           #print join('\t', @row)."\n";
           print join(',', @row)."\n";
    }

    $sqr->finish();
    $dbh->disconnect();
   
    print 'END...';
   
}unless (open (MYFILE, "$ARGV[0]")) {
    die ("cannot open input file $ARGV[0]\n");
    close(MYFILE);
}

xml如下

  1. <?xml version="1.0" encoding="GBK"?>  
  2. <outputlist timestamp="20090917232423">  
  3. <output>  
  4. <TopoMgr.NEAdditionalInfoType-array>  
  5.   <TopoMgr.NEAdditionalInfoType>  
  6.     <neId>134217729</neId>  
  7.     <neName>测试1 [1-1]</neName>  
  8.   </TopoMgr.NEAdditionalInfoType>  
  9.   <TopoMgr.NEAdditionalInfoType>  
  10.     <neId>134217731</neId>  
  11.     <neName>测试2 [1-2]</neName>  
  12.   </TopoMgr.NEAdditionalInfoType>  
  13. </TopoMgr.NEAdditionalInfoType-array>  
  14. </output>  
  15. </outputlist>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值