• # Un petit script perl

    Posté par . En réponse au message Shell script / parse XML, limites ?. Évalué à 2.

    Avec XML::LibXML :

    #!/usr/bin/perl 
    use strict;
    use warnings;
    use XML::LibXML;
    my $parser = XML::LibXML->new;
    my $struct = $parser->parse_file('data.xml');
    foreach my $host ($struct->findnodes('//Host')) {
     my ($name) = grep { $_->getName() eq 'name' } $host->getAttributes();
     foreach my $report ($host->findnodes('Report')) {
     my $id = $report->getAttribute('ID');
     my $niveau = $report->getAttribute('niveau');
     my ($title) = $report->findnodes('title');
     print $name->getValue(), ';', $id, ';', $niveau, ";", $title->to_literal, "\n" ;
     }
    }

    PS : c'est sans doute pas optimal, c'est la première fois que je parse du xml sérieusement et pas à coups de regexps ;)