[Omaha.pm] Database -> XML

Jay Hannah jhannah at omnihotels.com
Wed Apr 11 14:19:54 PDT 2007


Hmm... I wrote this this afternoon. Your thoughts? Good? Bad?

I think I like this idiom. Tight code, readable, maintainable. 

It's a one-way extract. From here I just map a bajillion more
tables/columns from our database and I'm done. If/when the subs get to
bulky I'll throw them to .pm files...

Ponder,

j


===========================================
#!/usr/bin/perl

use strict;
use XML::Twig;
use Omni::DB;
use Date::Calc qw(Today_and_Now);

my $dbh = Omni::DB::connect_prod_with_pw;

my $prop = "ATLCNN";
my $asof = sprintf("%04d%02d%02d%02d%02d%02d", Today_and_Now());

my $t = XML::Twig->new();
$t->set_pretty_print('indented');
$t->parse("<revolutiondata hotel='$prop' ver='1.50' seq='1'
asof='$asof'/>");
my $root = $t->root;
add_roominfo($root);
$t->print;

$dbh->disconnect;

sub add_roominfo {
   my ($root) = @_;
   my $roominfo = XML::Twig::Elt->new('roominfo');
   $roominfo->paste(last_child => $root) or die;
   my $sth = $dbh->prepare("select room_type, to_sell_qty from rooms
where prop = '$prop'");
   $sth->execute;
   while (my $href = $sth->fetchrow_hashref) {
      foreach (keys %$href) {
         $href->{$_} =~ s/\s+$//;
      }
      my $elt = XML::Twig::Elt->new( rmtype => {
         rt      => $href->{room_type},
         physcap => $href->{to_sell_qty},
      });
      $elt->paste(last_child => $roominfo) or die;
   }
   $sth->finish;
}
===========================================
<revolutiondata asof="20070411161656" hotel="ATLCNN" seq="1" ver="1.50">
  <roominfo>
    <rmtype physcap="1" rt="ATLANT"/>
    <rmtype physcap="0" rt="DCNSTE"/>
    <rmtype physcap="213" rt="DDN"/>
    <rmtype physcap="367" rt="DDN1"/>
    ...snip!...
  </roominfo>
</revolutiondata>
===========================================


More information about the Omaha-pm mailing list