IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Perl Discussion :

Récupérer les dates dans un fichier XML


Sujet :

Langage Perl

  1. #1
    Membre du Club
    Femme Profil pro
    Ingénieur d'études
    Inscrit en
    Mars 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études

    Informations forums :
    Inscription : Mars 2009
    Messages : 43
    Points : 48
    Points
    48
    Par défaut Récupérer les dates dans un fichier XML
    Bonjour,

    étant totalement novice en découpage de fichier XML à l'aide du langage Perl (j'assume), je me retrouve confrontée à un problème de taille : je n'arrive pas à récupérer le jour, le mois et l'année contenu dans mon fichier XML. J'ai beau chercher, XML::Twig veut absolument me retourner les informations sous une forme non lisible.

    Je vous fournis une portion de mon fichier XML (il est assez gros à lire) :
    Code xml : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <!DOCTYPE Entrezgene-Set PUBLIC "-//NLM//DTD NCBI-Entrezgene, 21st January 2005//EN" "http://www.ncbi.nlm.nih.gov/data_specs/dtd/NCBI_Entrezgene.dtd">
    <Entrezgene-Set>
     
    <Entrezgene>
      <Entrezgene_track-info>
        <Gene-track>
          <Gene-track_geneid>119</Gene-track_geneid>
          <Gene-track_status value="live">0</Gene-track_status>
          <Gene-track_create-date>
            <Date>
              <Date_std>
                <Date-std>
                  <Date-std_year>1998</Date-std_year>
                  <Date-std_month>7</Date-std_month>
                  <Date-std_day>27</Date-std_day>
                </Date-std>
              </Date_std>
            </Date>
          </Gene-track_create-date>
          <Gene-track_update-date>
            <Date>
              <Date_std>
                <Date-std>
                  <Date-std_year>2011</Date-std_year>
                  <Date-std_month>1</Date-std_month>
                  <Date-std_day>30</Date-std_day>
                  <Date-std_hour>1</Date-std_hour>
                  <Date-std_minute>11</Date-std_minute>
                  <Date-std_second>47</Date-std_second>
                </Date-std>
              </Date_std>
            </Date>
          </Gene-track_update-date>
        </Gene-track>
      </Entrezgene_track-info>

    Comme vous pouvez le constater, les dates sont découpées en année, mois et jour ou en année, mois, jour, heure, minute et seconde. Et pour une raison que j'ignore, je n'arrive pas du tout à récupérer l'information d'intérêt, alors que j'ai pu récupérer sans difficulté l'identifiant (Gene-track_geneid) du gène ainsi qu'un résumé pour ce gène (beaucoup plus loin dans le fichier).
    J'ai bien suivi le tutoriel sur Perl et les fichiers, dont le parsage de fichier XML, mais j'avoue que je reste dubitative devant le résultat obtenu.

    Voici mon script Perl actuel :
    Code perl : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    #!/usr/bin/perl -w                                  -*- Perl -*-
    #
    # $Id$
    #
    #
     
    use strict;
    use warnings;
    use XML::Twig;
     
    my $xml_file = "gene_file.xml";
     
    # Script de base de Djibril, source developpez.com (langage Perl)
    print "Version 2 - utilisation de XML::Twig\n\n";
    my $twig = new XML::Twig( Twig_handlers => { 'Gene-track' => \&gene_track,
    					     'Entrezgene_summary' => \&gene_summary,} );
     
     
    # Creation du fichier resultat
    my $file_out = 'out.txt';
    open OUT, "> $file_out" or ( warn "Can't open file $file_out\n!" and die);
     
    # creation d'un objet twig
    $twig->parsefile($xml_file);
     
    # Fermeture du fichier
    close OUT;
     
    # racine du XML
    # my $root = $twig->root;
     
    # Parcours des informations sur les gènes
    sub gene_track{
     
        my ( $root, $gene_info ) = @_;
     
        # GeneID
        print OUT "GeneID: ".$gene_info->field('Gene-track_geneid')."\n";
     
        # creation date
        my $creationdate = $gene_info->first_child('Gene-track_create-date');
     
        my $day = $creationdate->field('Date' => {'Date_std' => {'Date-std' => 'Date-std_day'}});
        my $month = $creationdate->field('Date' => {'Date_std' => {'Date-std' => 'Date-std_month'}});
        my $year = $creationdate->field('Date' => {'Date_std' => {'Date-std' => 'Date-std_year'}});
        print OUT "Create date: ".$day."/".
    	$month."/".
    	$year."\n";
     
        # last update
        my $update = $gene_info->first_child('Gene-track_update-date');
        print OUT "Modification date: ".$update->field('Date-std_day')."/".
    	$update->field('Date-std_month')."/".
    	$update->field('Date-std_year')."at ".
    	$update->field('Date-std_hour')." h ".
    	$update->field('Date-std_minute')."' ".
    	$update->field('Date-std_second')."''\n";
     
        $root->purge;
        return;
    }
    sub gene_summary{
        my ( $root, $gene_info ) = @_;
        # gene summary
        print OUT "Summary: ".$gene_info->text."\n";
     
        $root->purge;
        return;
     
    }

    Et mon fichier de sortie actuel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    GeneID: 119
    Create date: 1998727/1998727/1998727
    Modification date: //at  h ' ''
    Summary: Adducins are heteromeric proteins composed of different subunits referred to as adducin alpha, beta and gamma. The three subunits are encoded by distinct genes and belong to a family of membrane skeletal proteins involved in the assembly of spectrin-actin network in erythrocytes and at sites of cell-cell contact in epithelial tissues. While adducins alpha and gamma are ubiquitously expressed, the expression of adducin beta is restricted to brain and hematopoietic tissues. Adducin, originally purified from human erythrocytes, was found to be a heterodimer of adducins alpha and beta. Polymorphisms resulting in amino acid substitutions in these two subunits have been associated with the regulation of blood pressure in an animal model of hypertension. Heterodimers consisting of alpha and gamma subunits have also been described. Structurally, each subunit is comprised of two distinct domains. The amino-terminal region is protease resistant and globular in shape, while the carboxy-terminal region is protease sensitive. The latter contains multiple phosphorylation sites for protein kinase C, the binding site for calmodulin, and is required for association with spectrin and actin. Alternatively spliced transcript variants have been described. [provided by RefSeq]
    Comme vous pouvez le constater, je n'obtiens pas les informations souhaitées. Je pourrais m'amuser à passer par des expressions régulières mais je n'aurais pas à parser un seul fichier XML par la suite, aussi il faudrait réellement que j'arrive à comprendre ce qui ne va pas afin de récupérer les informations dont j'ai besoin et ce de la façon la plus automatisée que possible.

    En vous remerciant d'avance, cordialement, Norore.

  2. #2
    Membre averti

    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Janvier 2011
    Messages
    184
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Matériel informatique

    Informations forums :
    Inscription : Janvier 2011
    Messages : 184
    Points : 322
    Points
    322
    Par défaut
    XML+Perl remonte à loin dans mes souvenirs mais je peux te proposer ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    my $day = $creationdate->first_child('Date')->first_child('Date_std')->first_child('Date-std')->first_child('Date-std_day')->children_text;

  3. #3
    Membre du Club
    Femme Profil pro
    Ingénieur d'études
    Inscrit en
    Mars 2009
    Messages
    43
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Âge : 39
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Ingénieur d'études

    Informations forums :
    Inscription : Mars 2009
    Messages : 43
    Points : 48
    Points
    48
    Par défaut
    Citation Envoyé par Dimitry.e Voir le message
    XML+Perl remonte à loin dans mes souvenirs mais je peux te proposer ça :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    my $day = $creationdate->first_child('Date')->first_child('Date_std')->first_child('Date-std')->first_child('Date-std_day')->children_text;
    C'est exactement ça ! Merci infiniment !
    Finalement c'était encore plus bête que je ne pensais, j'ai juste cherché au mauvais endroit et sans penser à faire cette suite !

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [XSD] Récupérer les informations dans un fichier xsd (xml schema)
    Par gold_men84 dans le forum Format d'échange (XML, JSON...)
    Réponses: 2
    Dernier message: 04/03/2011, 18h50
  2. comment récupérer la plus récente date dans mon fichier XML?
    Par brain001 dans le forum XSL/XSLT/XPATH
    Réponses: 2
    Dernier message: 19/04/2009, 12h14
  3. [XPATH] Récupérer un élément dans un fichier XML
    Par be_tnt dans le forum XSL/XSLT/XPATH
    Réponses: 7
    Dernier message: 07/06/2007, 19h49
  4. [](VB) Récupérer les données dans un fichier .xml
    Par Furius dans le forum VBScript
    Réponses: 4
    Dernier message: 02/10/2005, 20h39

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo