Advertisement
Advertisement
| 04.04.2008 at 04:59AM PDT, ID: 23295688 |
|
[x]
Attachment Details
|
||
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: |
#!/usr/bin/perl -w
use XML::Twig;
use DBI;
my $dsn = 'DBI:mysql:database_name:localhost';
my $user = 'user';
my $pass = 'pass';
my $dbh = DBI->connect($dsn, $user, $pass) or die "Can not connect to the DB: $DBI::errstr\n";
$cur = $dbh->prepare('Test');
$cur->execute();
my $file = 'http://feeds.externalcompany.co.uk/?fc154d69-0c4d-42b8-93a4-4a20cg5225e1';
my $twig = XML::Twig->new();
$twig->parseurl($file);
my $root = $twig->root;
foreach my $article ($root->children('Article'))
{
my $itemid = $article->att('ID');
my $heading = $article->first_child_text('Heading');
#replacing single quote with '' in the heading tag to avoid problems with inserting into MySQL
$heading =~ s/'/''/g;
my $contents = $article->first_child_text('Contents');
#replacing single quote with '' in the Contents tag to avoid problems with inserting into MySQL
$contents =~ s/'/''/g;
my $articledate = $article->first_child_text('Date');
#Putting the date into the correct format for MySQL database
@splitdate = split(/\//, $articledate);
$date = $splitdate[2]."-".$splitdate[1]."-".$splitdate[0];
# delete the item from the database if it exists
my $sql = qq{ DELETE FROM Test WHERE ItemId=$itemid };
$dbh->do( $sql );
#insert the item into the database
my $sql = qq{ INSERT INTO Test (ItemID, Heading, Contents, Date) VALUES ($itemid, '$heading', '$contents', '$date') };
$dbh->do( $sql );
}
$cur->finish();
exit 0;
|