html2xml²

Transformer un html en xml, auquel l’on appliquera une transformation xslt pour au final avoir l’xml de nos rêves.

Il faut pour cela:

  • html2xml.pl (version 0.6) notre parseur perl
  • de la documentation sur xslt afin de créer une feuille de style .xsl

1) .html -> .xml

html2xml.pl fichier.html ficher.xml

2) .xml -> .xml

xsltproc feuilledestyle.xsl fichier.xml fichier_final.xml

Web

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

Recent Tweets

Catégories