I've been trying myself for 4 days now -- that's why I'm here.
If you don't have anything useful to say, please...
Main Topics
Browse All TopicsI have a (large - about 27k entries) xml file that I need to import into mysql.
It’s a dump of the Cisco error message database and I want to import it to use on the syslog server so that when a message comes in, it will display more info w/o having to go to CCO and look it up manually.
Here’s what the xml looks like:
<?xml version="1.0"?>
<cemdb class="simple">
<date>2006-06-28</date>
<error name="%">
<explanation></explanation
<action></action>
</error>
<error name="%">
<explanation>The POST has reported an incorrect memory size.</explanation>
<action>Replace the EAIM.</action>
</error>
<error name="%00:00:00:00:00:00">
<explanation>This is not a valid MAC address, and is probably being generated by non 802.1d
compliant hardware or software in the network. When there is a request to show the hosts the switch
has learned, it must sort the MAC addresses it has learned to reply to the request. The sort algorithm
cannot deal with a MAC address which is all zeroes, so the address will not be included in the table
of hosts which needs to be sorted. This means that even though the switch has learned a host with an
all zero MAC address, it will not show up in any display of hosts the switch has learned.</explanation>
<action>Track down the source of this bad address.</action>
</error>
<error name="%AAA-1-AAA_SESSION_L
<explanation>The AAA request is rejected because the maximum limit for concurrent AAA sessions
was reached.</explanation>
<action>If you purchased Cisco support through a Cisco reseller, contact the reseller
directly. If you purchased support directly from Cisco Systems, contact Cisco Technical Support.
Introduced Cisco MDS SAN-OS Release 1.3(1).</action>
</error>
<error name="%AAA-2-AAAMULTILINKE
<explanation>An AAA internal error has occurred.</explanation>
<action>Copy the error message exactly as it appears on the console or in the system
log, contact your Cisco technical support representative, and provide the representative with the
gathered information.</action>
</error>
<error name="%AAA-2-AAAMULTILINKE
<explanation>An AAA internal error has occurred.</explanation>
<action>Copy the error message exactly as it appears on the console or in the system log, contact your Cisco technical s
upport representative, and provide the representative with the gathered information.</action>
</error>
<error name="%AAA-2-AAA_NVRAM_UPG
<explanation>The accounting log could not be upgraded because of a problem with NVRAM. The
problem is described in the error message.</explanation>
<action>If you purchased Cisco support through a Cisco reseller, contact the reseller
directly. If you purchased support directly from Cisco Systems, contact Cisco Technical Support.
Introduced Cisco MDS SAN-OS Release 2.0(1b).</action>
</error>
<error name="%AAA-2-AAA_PROGRAM_E
<explanation>The AAA daemon is exiting.</explanation>
<action>If you purchased Cisco support through a Cisco reseller, contact the reseller
directly. If you purchased support directly from Cisco Systems, contact Cisco Technical Support.
Introduced Cisco MDS SAN-OS Release 1.3(1).</action>
</error>
</cemdb>
And here’s the mysql table:
CREATE TABLE cemdb (
id bigint(20) unsigned NOT NULL auto_increment,
name varchar(128) NOT NULL default '',
error text,
descr text,
action text,
datetime datetime default NULL,
PRIMARY KEY (id),
UNIQUE KEY name (name)
) ENGINE=MyISAM;
Can someone please write a perl script to import this into mysql for me?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I'm not sure how big the file is, but if it's small enough to load the entire thing into memory, then you can use XML::Simple to load the file:
http://search.cpan.org/~gr
Then use DBI to add to the DB
http://search.cpan.org/~ti
Here is some code to get you started reading the XML file...
#!/usr/bin/perl
use strict;
use Data::Dumper;
use XML::Simple;
my $ref = XMLin("/temp_ee/file1.xml"
print "Date=$ref->{date}\n";
foreach my $key (keys %{$ref->{'error'}}){
print "Name=$key\n";
print "Explanation=$ref->{error}
print "Action=$ref->{error}->{$k
#Any other fields....
}
Here's what I have.
This seems to be working, except that if a duplicate entry is found, it stops.
How can I check to see if $name exists and if it does, just do an update on the record?
#!/usr/bin/perl -w
use strict;
use lib ".";
use XML::Simple qw( :strict );
use DBI;
use Data::Dumper;
sub MY_DB_NAME () { "log" }
sub MY_DB_USER () { "root" }
sub MY_DB_PASS () { "pass" }
my $xml_file = 'emd.xml';
my $xml_ref = XMLin("$xml_file", ForceArray => 1, KeyAttr => []);
my $DEBUG = 1;
my $dbh = DBI->connect('dbi:mysql:'.
or die("Couldn't connect to mySQL ", MY_DB_NAME ," : $DBI::errstr");
my $cemdb_ins_sql = "INSERT INTO cemdb (name, error, descr, action) VALUES (?,?,?,?)";
my $sth = $dbh->prepare($cemdb_ins_s
foreach (@{ $xml_ref->{error}}) {
my($expl,$act,$name,$errms
if ($_->{name} =~ /(%.*?):\s?(.*)/) {
($name, $errmsg) = ($1,$2);
} else {
$name = $_->{name};
$errmsg = '';
}
print "$_->{name}\n" if ($DEBUG);
if ((ref $_->{explanation}) eq "ARRAY") {
foreach my $line ( @{ $_->{explanation}} ) {
$expl .= $line;
print "$line\n" if ($DEBUG);
}
} else {
$expl = $_->{explanation};
print "$_->{explanation}\n" if ($DEBUG);
}
if ((ref $_->{action}) eq "ARRAY") {
foreach my $line ( @{ $_->{action}} ) {
$act .= $line;
print "$line\n" if ($DEBUG);
}
} else {
$act = $_->{action};
print "$_->{action}\n" if ($DEBUG);
}
$sth->execute($name, $errmsg, $expl, $act) or die("$DBI::errstr");
}
Create a query and update statement near where you create your insert statement (I'm not a SQL expert, so the SQL might not be correct - I know for sure the UPDATE is not correct)
my $cemdb_query_sql = "SELECT name FROM cemdb WHERE (name='?')";
my $sth_query = $dbh->prepare($cemdb_query
my $cemdb_update_sql = "UPDATE cemdb SET name='?' WHERE name='?'";
my $sth_update = $dbh->prepare($cemdb_updat
my $cemdb_ins_sql = "INSERT INTO cemdb (name, error, descr, action) VALUES (?,?,?,?)";
my $sth = $dbh->prepare($cemdb_ins_s
Then where you have this:
$sth->execute($name, $errmsg, $expl, $act) or die("$DBI::errstr");
Do this:
$sth_query->execute($name)
$ary_ref = $sth->fetchall_arrayref;
if( $#{$ary_ref}>=0) {
#existing item found, use update
$sth_update->execute($name
}
else {
#no items found, insert
$sth->execute($name, $errmsg, $expl, $act) or die("$DBI::errstr");
}
Had to change it a little but this helped immensley, thanks Adam!
Here's the final code in case anyone else ever needs it:
#!/usr/bin/perl -w
use strict;
use lib ".";
use XML::Simple qw( :strict );
use DBI;
use Data::Dumper;
sub MY_DB_NAME () { "log" }
sub MY_DB_USER () { "root" }
sub MY_DB_PASS () { "somepass" }
my $xml_file = 'emd.xml';
my $xml_ref = XMLin("$xml_file", ForceArray => 1, KeyAttr => []);
my $DEBUG = 0;
my $dbh = DBI->connect('dbi:mysql:'.
or die("Couldn't connect to mySQL ", MY_DB_NAME ," : $DBI::errstr");
my $cemdb_query_sql = "SELECT id FROM cemdb WHERE name=?";
my $sth_query = $dbh->prepare($cemdb_query
my $cemdb_update_sql = "UPDATE cemdb SET error=?, descr=?, action=? WHERE name=?";
my $sth_update = $dbh->prepare($cemdb_updat
my $cemdb_ins_sql = "INSERT INTO cemdb (name, error, descr, action) VALUES (?,?,?,?)";
my $sth = $dbh->prepare($cemdb_ins_s
foreach ( @{ $xml_ref->{error} } ) {
my($expl,$act,$name,$errms
$name = $_->{name};
print "$_->{name}\n" if ($DEBUG);
$errmsg = $_->{message};
print "$_->{message}\n" if ($DEBUG);
if ((ref $_->{explanation}) eq "ARRAY") {
foreach my $line ( @{ $_->{explanation} } ) {
$expl .= $line;
print "$line\n" if ($DEBUG);
}
} else {
$expl = $_->{explanation};
print "$_->{explanation}\n" if ($DEBUG);
}
if ((ref $_->{action}) eq "ARRAY") {
foreach my $line ( @{ $_->{action} } ) {
$act .= $line;
print "$line\n" if ($DEBUG);
}
} else {
$act = $_->{action};
print "$_->{action}\n" if ($DEBUG);
}
#$sth->execute($name, $errmsg, $expl, $act) or die("$DBI::errstr");
$sth_query->execute($name)
my $ary_ref = $sth_query->fetchall_array
if( $ary_ref >=0) {
#existing item found, use update
$sth_update->execute($name
print "Updating $name\n"
}
else {
#no items found, insert
$sth->execute($name, $errmsg, $expl, $act) or die("$DBI::errstr");
print "Inserting $name\n"
}
}
Business Accounts
Answer for Membership
by: stefan73Posted on 2006-06-29 at 09:29:45ID: 17011064
http://www.rentacoder.com ;-)
Try it yourself and ask when you have specific problems.