Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The story so far:
Many thanks to all of the monks who came up with answers to my original post. I went with the DBI / DBD::ODBC approach because it was closest to my original idea and I had all of the components already installed.

The code below lacks any real error handling and doesn't log any error/status information. I will be doing that later.

Be gentle with your comments!
Inman

#! /usr/bin/perl -w # # Daniel Inman # # 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: Flattening Access DB to XML by inman
in thread Flattening Access DB to XML by inman

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 04:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found