#!/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如下
- <?xml version="1.0" encoding="GBK"?>
- <outputlist timestamp="20090917232423">
- <output>
- <TopoMgr.NEAdditionalInfoType-array>
- <TopoMgr.NEAdditionalInfoType>
- <neId>134217729</neId>
- <neName>测试1 [1-1]</neName>
- </TopoMgr.NEAdditionalInfoType>
- <TopoMgr.NEAdditionalInfoType>
- <neId>134217731</neId>
- <neName>测试2 [1-2]</neName>
- </TopoMgr.NEAdditionalInfoType>
- </TopoMgr.NEAdditionalInfoType-array>
- </output>
- </outputlist>