Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
There follows some old code that I had knocking around on my machine from a proof of concept.

This code was from a risk management project where there was a concern that the content of Access databases could not be analysed by the risk management search tool. The idea was to locate and dump any MDB to an XML file which could then be indexed without having to know anything about the database schema.

The code is probably well dodgy...

#! /usr/bin/perl -w # # Dump access databases to XML using DBI and DBD::ODBC use strict; use warnings; use File::Find; use DBI; use IO::File; use XML::Writer; use Cwd; #use Data::Dumper; my $findpath = $ARGV[0] ? $ARGV[0] : '.'; my $output; my $xmlwriter; my $driver = "Microsoft Access Driver (*.mdb)"; # scan all files looking for *.mdb find({ wanted => \&processDatabase}, $findpath); # Process MDB files # Produce and XML file in the same directory with the same name but xm +l extension. sub processDatabase { return unless $File::Find::name =~ /([^\/]*\.mdb)/i; my $database = cwd . "/$1"; my $xmlfile = $database; $database =~ s!/!\\!g; $xmlfile =~ s/\.mdb$/.xml/i; $output = new IO::File(">$xmlfile"); $xmlwriter = new XML::Writer(OUTPUT => $output, DATA_MODE => 1, DA +TA_INDENT => 4); $xmlwriter->xmlDecl("UTF-8"); $xmlwriter->startTag ("Database", "path"=>"$database"); exportDatabase ($database); $xmlwriter->endTag ("Database"); $xmlwriter->end(); $output->close(); } # Export the database sub exportDatabase { my $database = shift; print "Exporting: $database\n"; my $dsn = "dbi:ODBC:driver=$driver;dbq=$database"; my $dbh = DBI->connect("$dsn") or die "Couldn't open database: $DBI::errstr; stopped"; my $sth = $dbh->table_info( "", "", "", "TABLE" ); while ( my ($catalog, $schema, $table, $type) = $sth->fetchrow_arr +ay() ) { if ($table) { print "Exporting $table\n"; my $sql = "select * from $table"; # Prepare the SQL query for execution my $sth = $dbh->prepare ("$sql") or die "Couldn't prepare statement:$DBI::errstr; stopp +ed"; # Execute the query $sth->execute() or die "Couldn't execute statement: $DBI:: +errstr; stopped"; $xmlwriter->startTag ("Table", "name"=>"$table"); # Fetch each row and print it while ( my (@row) = $sth->fetchrow_array() ) { $xmlwriter->startTag ("Row"); foreach (@row) { $xmlwriter->dataElement ("Column", $_); } $xmlwriter->endTag ("Row"); } $xmlwriter->endTag ("Table"); } } # Disconnect from the database $dbh->disconnect(); }

In reply to Re: Convert MDB File To Txt File by inman
in thread Convert MDB File To Txt File by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (3)
    As of 2014-09-17 02:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (56 votes), past polls