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

Format d'échange (XML, JSON...) Java Discussion :

Sortie fichier XML


Sujet :

Format d'échange (XML, JSON...) Java

  1. #1
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut Sortie fichier XML
    Bonjour à tous,

    j'ai fait un programme pour traiter du texte (segmenter le texte en phrases) lequel génère en sortie un ficher XML.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    String xmlOutput = DocumentStaxUtils.toXml(currDoc);
    FileWriter writer = new FileWriter("fichier.xml");
    writer.write(xmlOutput, 0, xmlOutput.length());
    writer.flush();
    writer.close();
    En exécutant ce programme il y a un avertissement de configuration de log4j, donc je l'ai desactivé
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    org.apache.log4j.BasicConfigurator.configure();
     
    org.apache.log4j.Level level = org.apache.log4j.Level.OFF;
     
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger();
     
    logger.setLevel(level);
    Le problème est qu'il n'y a pas de fichier en sortie et je ne sais pas d'où vient le problème, bien qu'il n'y ait pas d'erreur.

    Merci d'avance pour votre aide.

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    As-tu essayé en donnant un chemin absolu, et non relatif ? Peut-être que le fichier existe, mais que tu ne l'as pas trouvé ?

  3. #3
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    Non j'ai pas essayé
    j'ai cherché dans le dossier de mon projet

  4. #4
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    J'ai essayé avec le chemin absolu
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    FileWriter writer = new FileWriter("C:\\Documents and Settings\\Administrateur\\Bureau\fichier.xml");
    mais ça ne fonctionne pas.

  5. #5
    Membre confirmé
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Octobre 2008
    Messages
    380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Octobre 2008
    Messages : 380
    Points : 480
    Points
    480
    Par défaut
    Normalement tu devrais avoir une exception qui est remontée en utilisant le FileWriter que tu as fourni, car il manque un caractère '\' avant "fichier.xml".

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
     FileWriter writer = new FileWriter("C:\\Documents and Settings\\Administrateur\\Bureau\fichier.xml");
    devrait être
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    FileWriter writer = new FileWriter("C:\\Documents and Settings\\Administrateur\\Bureau\\fichier.xml");

  6. #6
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    Juste c'est une faute de frappe ici.
    Le code est juste et ça marche pas.

  7. #7
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Juste avant le flush, écrit une chaîne bidon dans le fichier. Et affiche également la taille de la chaîne "xmlOutput".
    Même si la chaîne est vide, le fichier doit être construit. Donne le code complet.
    Es-tu certain que l'application exécute ce code ?

  8. #8
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    oui normalement le code s'exécute
    voila ce qui est affiché en console
    Code : 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
    71
    72
    73
    74
    75
    76
    77
    78
     
    run:
    Initialising GATE...
    Chargement Sentence Splitter...  
    Using C:\Programs\GATE-5.0-beta1 as GATE home
    Using C:\Programs\GATE-5.0-beta1\plugins as installed plug-ins directory.
    Création du corpus...  
    C'est parti...  
    Terminé !...  
    Using C:\Documents and Settings\Administrateur\gate.xml as site configuration file.
    Using .\user-gate.xml as user configuration file
    Using C:\Documents and Settings\Administrateur\gate.session as user session file
    CREOLE plugin loaded: file:/C:/Programs/GATE-5.0-beta1/plugins/TreeTagger/
    CREOLE plugin loaded: file:/C:/Programs/GATE-5.0-beta1/plugins/ANNIE/
    CREOLE plugin loaded: file:/C:/Programs/GATE-5.0-beta1/plugins/Information_Retrieval/
    CREOLE plugin loaded: file:/C:/Programs/GATE-5.0-beta1/plugins/Jape_Compiler/
             1) C:\\Documents and Settings\\Administrateur\\Mes documents\\NetBeansProjects\\JavaApplication2\\GaidaMeher.pdfDocument "/C:/Documents and Settings/Administrateur/Mes documents/NetBeansProjects/JavaApplication2/GaidaMeher.pdf" :
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=0; offset=506)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=2; offset=677)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=4; offset=1016)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=6; offset=1426)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=8; offset=1458)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=10; offset=1864)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=12; offset=1967)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=14; offset=2193)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=16; offset=2235)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=18; offset=2304)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=20; offset=2352)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=22; offset=2547)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=24; offset=2614)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=26; offset=2847)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=28; offset=2908)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=30; offset=3154)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=32; offset=3219)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=34; offset=3517)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=36; offset=3600)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=38; offset=3912)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=40; offset=4339)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=42; offset=4399)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=44; offset=4537)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=46; offset=4610)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=48; offset=4812)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=50; offset=4842)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=52; offset=4900)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=54; offset=4951)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=56; offset=4986)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=58; offset=5050)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=60; offset=5208)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=62; offset=5370)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=64; offset=5505)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=66; offset=5528)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=68; offset=5554)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=70; offset=5650)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=72; offset=5849)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=74; offset=5888)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=76; offset=5993)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=78; offset=6374)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=80; offset=6495)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=82; offset=6623)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=84; offset=6734)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=86; offset=6843)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=88; offset=7096)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=90; offset=7150)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=92; offset=7573)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=94; offset=8710)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=96; offset=9762)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=98; offset=10225)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=100; offset=10474)
    "OCT" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=102; offset=10863)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=104; offset=10920)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=106; offset=11883)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=108; offset=12495)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=110; offset=12893)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=112; offset=13568)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=114; offset=13777)
    "et" : Lookup - {majorType=splitter_abbreviation} (NodeImpl: id=116; offset=13884)
     
    BUILD SUCCESSFUL (total time: 4 seconds)
    Par contre j'ai pas compris ton idée

  9. #9
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    Citation Envoyé par soumti84 Voir le message
    Par contre j'ai pas compris ton idée
    L'idée est d'ajouter un :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    writer.write( "coucou" );
    histoire d'être sur que tu écris des choses dans le fichier.

    Par contre, la trace d'exécution ne sert pas à grand chose car tu n'as pas encore donné le code complet

  10. #10
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    c'est la classe SenSplitter
    Code : 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
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package javaapplication3;
     
    /**
     *
     * @author Administrateur
     */
    import java.io.File;
    import java.io.IOException;
    import java.util.HashSet;
    import java.util.Iterator;
    import java.util.Set;
    import gate.*;
    import gate.corpora.DocumentStaxUtils;
    import gate.util.GateException;
    import gate.util.InvalidOffsetException;
    import java.io.FileWriter;
    public class SenSplitter {
    public String encoding = "UTF-8";
    public String outDir = System.getenv("HOME") + File.separator + "tmp";
    public  Corpus createCorpus(String[] files) throws GateException {
    		gate.Corpus corpus = Factory.newCorpus("Corpus");
     
    		// Parcours des fichiers
    		for (int file = 0; file < files.length; file++) {
    			System.out.print("\t " + (file + 1) + ") " + files[file]);
    			// Ajout dans le corpus
    			try {
    				corpus.add(Factory.newDocument(new File(files[file]).toURL()));
    			} catch (gate.creole.ResourceInstantiationException e) {
    				System.out.println(" -- échec (" + e.getMessage() + ")");
    			} catch (Exception e) {
    				System.out.println(" -- " + e.getMessage());
    			}
    		}
    		return corpus;
    	}
    public  void ecrireAnn(Corpus corpus) throws IOException {
    		// On crée un iterateur sur les documents du corpus
    		Iterator iter = corpus.iterator();
    		// Parcours de tous les documents du corpus
    		while (iter.hasNext()) {
    			Document currDoc = (Document) iter.next();
     
    			String docXMLString = null;
    			// Les annotations à conserver
    			Set annotationsToWrite = new HashSet();
     
    			// Seul l'AnnotationSet par défaut (unnamed) est utilisé ici
    			AnnotationSet annotations = currDoc.getAnnotations();
    			for (int i = 0; i < annotations.size(); i++) {
    				Annotation annotation = annotations.get(i);
    				// On ne conserve que les prénoms
    				// (Lookup->majorType = prenoms
    				if (annotation != null
    						&& annotation.getType().equals("Lookup")) {
    					annotationsToWrite.add(annotation);
    				}
    			}
     
    			// Création du XML avc les annotations récupérées
    			//docXMLString = currDoc.toXml(annotationsToWrite);
                String xmlOutput = DocumentStaxUtils.toXml(currDoc);
                FileWriter writer = new FileWriter("C:\\Documents and Settings\\Administrateur\\Bureau\\fichier.xml");
                writer.write(xmlOutput, 0, xmlOutput.length());
                writer.flush();
                writer.close();
    	}}
    public void afficherAnn(Corpus corpus)
    			throws InvalidOffsetException {
    		// On crée un iterateur sur les documents du corpus
    		Iterator iter = corpus.iterator();
    		// Parcours de tous les documents du corpus
    		while (iter.hasNext()) {
    			Document currDoc = (Document) iter.next();
     
    			// Affichage des infos sur le document
    			System.out.println("Document \""
    					+ currDoc.getSourceUrl().getFile() + "\" :");
     
    			// Parcours de l'ensemble des annotations du corpus
    			AnnotationSet annotations = currDoc.getAnnotations();
    			for (int i = 0; i < annotations.size(); i++) {
    				Annotation annotation = annotations.get(i);
    				// On ne conserve que les prénoms
    				// (Lookup->majorType = prenom
    				if (annotation != null
    						&& annotation.getType().equals("Lookup"))
    						 {
    					// Affichage
    					System.out.println("\""
    							+ currDoc.getContent().getContent(
    									annotation.getStartNode().getOffset(),
    									annotation.getEndNode().getOffset()) + "\""
    							+ " : " + annotation.getType() + " - "
    							+ annotation.getFeatures() + " ("
    							+ annotation.getStartNode() + ")");
    				}
    			}
    			System.out.println();
    		}
    	}
     
     
    }
    et voila le main
    Code : 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
    71
    72
     
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package javaapplication3;
    import java.io.File;
     
    import java.io.IOException;
    import gate.*;
    import gate.creole.SerialAnalyserController;
    import gate.util.GateException;
    import gate.util.Out;
    import java.net.MalformedURLException;
    /**
     *
     * @author Administrateur
     */
    public class Main {
        private static FeatureMap params;
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) throws GateException, MalformedURLException {
            org.apache.log4j.BasicConfigurator.configure();
     
    org.apache.log4j.Level level = org.apache.log4j.Level.OFF;
     
    org.apache.log4j.Logger logger = org.apache.log4j.Logger.getRootLogger();
     
    logger.setLevel(level);
            // TODO code application logic here
            SenSplitter Sp=new SenSplitter();
            String encoding = "UTF-8";
            String[] files = args;
     
     Out.prln("Initialising GATE...");
     System.setProperty("gate.home", "C:\\Program Files\\GATE-5.0-beta1\\");
     
        Gate.setGateHome(new File("C:/Programs/GATE-5.0-beta1/"));
    Gate.setPluginsHome(new File("C:/Programs/GATE-5.0-beta1/plugins/"));
    Gate.setSiteConfigFile(new File("C:/Documents and Settings/Administrateur/gate.xml"));
    Gate.setUserConfigFile(new File("./user-gate.xml"));
    Gate.init();
    Corpus corpus = Sp.createCorpus(files);
    SerialAnalyserController pipeline = (SerialAnalyserController) Factory
    				.createResource("gate.creole.SerialAnalyserController");
    Gate.getCreoleRegister().registerDirectories(
    				new File(Gate.getPluginsHome(), "ANNIE").toURL());
    System.err.println("Chargement Sentence Splitter...  ");
    		params = Factory.newFeatureMap();
    		params.put("encoding", encoding);
    		ProcessingResource splitter = (ProcessingResource) Factory
    				.createResource("gate.creole.splitter.SentenceSplitter");
    		pipeline.add(splitter);
            System.err.println("Création du corpus...  ");
    		pipeline.setCorpus(corpus);
     
    		// C'est parti
    		System.err.println("C'est parti...  ");
    		pipeline.execute();
     
    		// Affichage
    		System.err.println("Terminé !...  ");
    		Sp.afficherAnn(corpus);
    		// ecrirePrenoms(corpus);
     
        }
     
    }

  11. #11
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    J'ai essayé d'écrire dans le fichier mais en vain.
    Le fichier ne se crée pas.
    C'est étonnant.

  12. #12
    Membre du Club
    Inscrit en
    Avril 2007
    Messages
    120
    Détails du profil
    Informations forums :
    Inscription : Avril 2007
    Messages : 120
    Points : 52
    Points
    52
    Par défaut
    J'ai compris la faute j'ai pas fait attention !
    Merci à vous

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 28/02/2012, 18h30
  2. [BOOST.Test] Fichier XML de sortie de tests
    Par vdaanen dans le forum Boost
    Réponses: 8
    Dernier message: 05/09/2011, 13h34
  3. Réponses: 10
    Dernier message: 17/04/2011, 16h19
  4. [GMF] Comment générer un fichier XML en sortie
    Par gerard_kh dans le forum Eclipse Modeling
    Réponses: 2
    Dernier message: 07/05/2010, 01h45
  5. Sortie Jms en String d'un fichier XML
    Par ctardella dans le forum Développement de jobs
    Réponses: 0
    Dernier message: 12/08/2009, 20h21

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