I worked up this example as an exercise to see how easy or difficult it is to combine XML with DBI. I found XML::Simple to meet all of my current needs, and DBIx::Recordset is flexible enough to handle changes in my table structures. I used PerlMonks XML generators for my sample data.

Read the comments for a blow-by-blow explanation of each part.

In summary, I found XML::Simple and DBIx::Recordset easy to combine, and I will move forward with my project using these two CPAN modules.

--
hiseldl

Update: added: , forcearray => ['NODE'] ...as suggested by grantm.

#!/usr/bin/perl -w use strict; use XML::Simple; use LWP::Simple; use DBIx::Recordset; use vars qw/ %xp_set $xp_set @xp_set $node_set %node_set @node_set $nodelog_set %nodelog_set @nodelog_set /; ############################################################ # # Database configuration # # See the table structure code after at the end. # my $data_source = "DBI:mysql:mydb"; my $username = "user"; my $auth = "pass"; my %attr = (); # can be (RaiseError=>1); ############################################################ # # Put your PerlMonks information into the xpinfo table # before running this script :) # *xp_set = DBIx::Recordset->Search({ '!DataSource'=> $data_source, '!Username' => $username, '!Password' => $auth, '!DBIAttr' => \%attr, '!Table' => 'xpinfo', }); ############################################################ # # See http://www.perlmonks.org/index.pl?node_id=72241 for # more information about these URL's # # From the Recordset that we just declared above, we can # use the hash from the glob to access our data, as in # $xp_set{column_name}. DBIx::Recordset gets a ++. my $XP_URL = "http://www.perlmonks.org/index.pl". "?node=XP%20xml%20ticker&op=login&user=". $xp_set{foruser}. "&passwd=". $xp_set{password}; my $USER_NODES_URL = "http://www.perlmonks.org/index.pl". "?node=user%20nodes%20info%20xml%20generator". "&op=login&user=". $xp_set{foruser}. "&passwd=". $xp_set{password}; # Create a hashref from the XML data, and make sure the # "id" element is part of the record hash my $xs = new XML::Simple(keyattr=>{NODE=>"+id"}, forcearray => ['NODE']); # Get the XML from PerlMonks :) # # XML::Simple turns the XML into a hashref structure, # that, once you use it a few times, not only makes # sense, but it is easy; XML::Simple gets a ++. my $xpref = $xs->XMLin( get($XP_URL) ); my $userref = $xs->XMLin( get($USER_NODES_URL) ); # create an alias to the nodes as a hashref my $nodes = $userref->{NODE}; ############################################################ # # Prepare the User Nodes recordset object # *node_set = DBIx::Recordset->Search({ '!DataSource'=> $data_source, '!Username' => $username, '!Password' => $auth, '!DBIAttr' => \%attr, '!Table' => 'nodes', }); # Yes this is a hack to get the time to look like a datetime # that MySQL will recognize my @now = localtime; my $now = (1900+$now[5])."-".($now[4]+1)."-".$now[3]." ". $now[2].":".$now[1].":".$now[0]; ############################################################ # # Prepare the Reputation Log recordset object # # --note that the noderep_log table does not have a primary # --key, this is so that we can use this table to track time # --series data, such as increase/decrease reputation of a # --user's node. If we had a primary key on this table, we # --would end up with one row per node id which is not what # --we want; we want one row per time-period, per node. *nodelog_set = DBIx::Recordset->Search({ '!DataSource'=> $data_source, '!Username' => $username, '!Password' => $auth, '!DBIAttr' => \%attr, '!Table' => 'noderep_log', }); # cycle through the nodes, insert any new nodes and add an # entry to the noderep_log table for historical tracking. foreach (keys %{$nodes} ) { $nodes->{$_}->{date_added} = undef; eval { $nodes->{$_}->{date_added} = $now; $node_set->Insert( {%{$nodes->{$_}} }); $nodelog_set->Insert( {%{$nodes->{$_}} }); }; if($@) { print STDERR $@,"\n"; } } __END__ # MySQL table structure # # # Table structure for table 'noderep_log' # CREATE TABLE noderep_log ( id varchar(32) DEFAULT '' NOT NULL , reputation int(11) , date_added datetime DEFAULT '0000-00-00 00:00:00' ); # # Table structure for table 'nodes' # CREATE TABLE nodes ( id varchar(32) DEFAULT '' NOT NULL , content varchar(255) , createtime datetime , PRIMARY KEY (id) ); # # Table structure for table 'xpinfo' # CREATE TABLE xpinfo ( id varchar(32) DEFAULT '' NOT NULL , site varchar(255) , sitename varchar(255) , foruser varchar(32) , password varchar(32) , PRIMARY KEY (id) );

In reply to DBIx::Recordset and XML::Simple Example by hiseldl

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.