Nevamonk has asked for the wisdom of the Perl Monks concerning the following question:
I seek your wisdom regarding the copying of an XML-file. Basically, this script will read incoming XML files, matches them to a database, where some ID's may need to be overwritten, but i need to keep the old data file to. So i was thinking of copying the file and matching the new file to database, overwriting the attributes in both name and value. Now how do i copy an XML file and bind it to a variable?
use warnings ; use strict ; use XML::LibXML; use DBI ; use File::Copy qw(copy); my $xmlInputFile = ""; my $dbConnection =""; my $parser = XML::LibXML->new(); my $databasename = "test_db"; my $hostname = "localhost"; my $username = "rrdtool"; my $password = ""; my $elemNameID; my $elemID; my $elemValueID; my $elemValueList; my $xmlOutputFile; my $matchRefFile; #=========================================# # SUB-Routines # #=========================================# sub connectDatabase { my $bConnected = 0; $dbConnection = DBI->connect( "dbi:mysql:database=$databasename;ho +st=$hostname", $username, $password ); if ( $dbConnection ) { $bConnected = 1; } else { print "There was an error connecting to : $databasename => $DB +I::errstr\n"; } return $bConnected; } #=========================================# sub WriteToLog { my ( $logline ) = @_; open ( LOGFILE, '>>RRDTool_translate_test.log'); print LOGFILE $logline; close ( LOGFILE ); } #=========================================# sub createNewXmlFile { #open my $matchRefFile, '<', ""; #binmode $matchRefFile; # drop all PerlIO layers possibly created + by a use open pragma #my $doc = XML::LibXML->load_xml(IO => $matchRefFile); #open my $xmlOutputFile, '>', 'xmlOutputFile.xml'; #binmode $xmlOutputFile; # as above # $doc->toFH($xmlOutputFile); # or #print {$xmlOutputFile} $doc->toString(); copy $xmlInputFile, $xmlOutputFile; rename $xmlOutputFile, 'xmlOutputFile.xml'; } #=========================================# sub matchNodeDB { if ( 1 == connectDatabase) { my $sqlSelectQuery = ( "SELECT IF NOT EXISTS * FROM XMLelementen + WHERE name ='$elemNameID' ELSE INSERT INTO XMLelementen VALUES ('$el +emNameID')"); my $query_handle = connectDatabase->prepare ( $sqlSelectQuery ); $query_handle->execute(); $query_handle->bind_columns(undef, \$elemID); return $elemID; } else { WriteToLog ("failed to get / insert $elemNameID from Database.\n +"); die (WriteToLog ("exitting on failed matchNodeDB sub for [$elemN +ameID]\n Exitting...\n")); } } #=========================================# sub WriteToFile { open ( OutputFile, '>>$xmlOutputFile'); $elemNameID->set_att(p => $elemID); $elemValueID->set_att(p => $elemID); WriteToLog ("Overwriting attribute ID's for $matchRefFile"); close ( OutputFile ); } #=========================================# sub escapeQuotes { my ( $inString ) = @_; # Escape single quotes $inString =~ s/\'/\\'/g; # Escape double quotes $inString =~ s/\"/\\"/g; # Escape the @-symbol $inString =~ s/\@/\\@/g; return $inString; } #=========================================# sub readXmlFile { my ( $xmlDir, $xmlInputFile ) = @_; WriteToLog ( "Parsing: $xmlInputFile"); my $matchRefFile = $parser->parse_file ("$xmlDir/$xmlInputFile"); createNewXmlFile ( $xmlDir, $xmlInputFile, $xmlOutputFile ); my $qryElemName = ("/mdc/md/mi/mt"); my $qryElemValue = ("/mdc/md/mi/r"); for my $manElemList ( $matchRefFile->findnodes ( $qryElemName )) { my $elemNameID = $manElemList->getAttribute( "p"); foreach ($elemNameID) { my $elemValueList = ( $matchRefFile->findnodes ( $qryElemValu +e )); my $elemValueID = ( $elemValueList->getAttribute ( "p" )); if ($elemNameID == $elemValueID) { matchNodeDB ($elemNameID); WriteToLog ("Binding [$elemID] to [$elemNameID]"); WriteToFile ( $elemID, $elemNameID, $elemValueID, $xmlOutp +utFile ); } } WriteToLog ( "\n"); } WriteToLog ( "\n"); } #=========================================# + sub readXmlDir { my ( $xmlDirectory ) = @_; opendir (DIR, $xmlDirectory ) or die $!; while ( my $xmlFile = readdir( DIR ) ) { next unless ( -f "$xmlDirectory/$xmlFile" ); next unless ( $xmlFile =~ m/\.xml$/ ); WriteToLog ( "Matching: [$xmlFile] to database.\n"); readXmlFile ( $xmlDirectory, $xmlFile ); } closedir ( DIR ); } #=========================================# ## < MAIN ENTRY > WriteToLog( "RRD Tool Translate test Version: $version ( $copyright )\ +n"); WriteToLog( "Matching $xmlInputFile to Database: $databasename \n"); if ( 1 == connectDatabase() ) { readXmlDir( "/home/rrdtool") } else { WriteToLog ( "No Database connection established, exiting") }
|
|---|