Parsing XML et insertions MySQL avec Perl

Installation des modules DBI et XML::XPath via CPAN

perl -MCPAN -e 'install DBI'
perl -MCPAN -e 'install XML::XPath'

ou

apt-get install libxml-xpath-perl

data.xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
<people>
<name>alexandre</name>
<surname>alex</name>
<age>30</age>
</people>
<people>
<name>alice</name>
<surname>ali</name>
<age>30</age>
</people>
</data>

parser.pl
#!/usr/bin/perl -w
use strict;
use DBI;
use XML::XPath;
use XML::XPath::XMLParser;
my $dbh = DBI->connect ("DBI:mysql:dbname",
"mysql_user", "mysql_password",
{ RaiseError => 1, PrintError => 0});
my $xp = XML::XPath->new (filename => $ARGV[0]);
my $nodelist = $xp->find ("//people");
foreach my $row ($nodelist->get_nodelist ())
{
$dbh->do (
"INSERT INTO test (field1, field2, field3) VALUES (?,?,?)",
undef,
$row->find ("field1")->string_value (),
$row->find ("field2")->string_value (),
$row->find ("field3)")->string_value ()
);
}
$dbh->disconnect ();

Using XML with MySQL

Autres articles susceptibles de vous intéresser :

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Tweets

Catégories